开发者社区> 问答> 正文

Tensorflow:Logits和标签必须具有相同的第一个维度

'distributions'列对应于一个独特的方程组,我试图将其压缩成SageMath中的一系列数字。'probs'列对应于是否应该根据它所在的行和列,通过给定的单项式来给定方程式多余。上面仅用于概述,与我的实际问题无关。

无论如何,这是我的代码。我试着用注释尽可能地解释它。

import csv
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.keras as keras

distribution_train = []
probs_train = []

x_train = []

y_train = []

with open('tftest.csv') as csv_file:

csv_reader = csv.reader(csv_file, delimiter=',')

for row in csv_reader:
    distribution_train.append(row[0])
    probs_train.append(row[1])

'''
Get rid of the titles in the csv file
'''
distribution_train.pop(0)
probs_train.pop(0)

'''
For some reason everything in my csv file is stored as strings.
The below function is to convert it into floats so that TF can work with it.
'''
def num_converter_flatten(csv_list):

f = []
for j in range(len(csv_list)):
    append_this = []
    for i in csv_list[j]:
        if i == '1' or i == '2' or i == '3' or i == '4' or i == '5' or i == '6' or i == '7' or i == '8' or i =='9' or i =='0':
            append_this.append(float(i))
    f.append((append_this))

return f

x_train = num_converter_flatten(distribution_train)
y_train = num_converter_flatten(probs_train)

x_train = tf.keras.utils.normalize(x_train, axis=1)
y_train = tf.keras.utils.normalize(y_train, axis=1)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

'''
I'm making the final layer 80 because I want TF to output the size of the
'probs' list in the csv file
'''

model.add(tf.keras.layers.Dense(80, activation=tf.nn.softmax))

model.compile(optimizer='adam',

          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
但是,当我运行我的代码时,我收到以下错误。

tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [32,80] and labels shape [2560]
[[{{node loss/output_1_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}} = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64, _class=["loc:@train...s_grad/mul"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](loss/output_1_loss/Log, loss/output_1_loss/Cast)]]

展开
收起
一码平川MACHEL 2019-01-16 18:15:54 8301 0
1 条回答
写回答
取消 提交回答
  • 我设法让您的代码处理一些更改,由于您使用了“sparse_categorical_crossentropy”,似乎发生了错误。我不知道你为什么使用这个,因为你的课程似乎并不是独家的,即。对于每个条目,您在tftest.csv中的几行中得分为'1'。此外,您不应该标准化您的标签。我做了这些改变:

    x_train = num_converter_flatten(distribution_train)
    y_train = num_converter_flatten(probs_train)

    x_train = tf.keras.utils.normalize(x_train, axis=1)
    y_train = np.array(y_train)#tf.keras.utils.normalize(y_train, axis=1)
    进一步向下:

    model.add(tf.keras.layers.Dense(80, activation=tf.nn.sigmoid))

    model.compile(optimizer='adam',

              loss='categorical_crossentropy',
              metrics=['accuracy'])

    同样,由于您的类似乎不是独占的,因此您不应使用softmax激活。

    但是既然代码现在有效,你就可以开展优化工作了(它似乎没有为我运行的5个时代进行良好的训练)。

    2019-07-17 23:25:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
使用TensorFlow搭建智能开发系统自动生成App UI 立即下载
从零到一:IOS平台TensorFlow入门及应用详解 立即下载
从零到一:IOS平台TensorFlow入门及应用详解(附源 立即下载