Tensorflow:Logit和标签的第一维必须相同

时间:2019-01-16 02:38:16

标签: python tensorflow machine-learning

我是TF机器学习的新手。我有此数据集,该数据集已生成并导出到.csv文件。在这里:tftest.csv

“分布”列对应于一个独特的方程式系统,我尝试将其简化为SageMath中的一系列数字。 “概率”列对应于是否应该根据方程式的给定单项式基于其所在的行和列来给定一个方程式,所以上面的内容仅供参考,与我的实际问题无关。 >

无论如何,这是我的代码。我试图通过注释尽力解释它。

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)]]

我在网上搜索了此错误,但似乎无法理解为什么会出现此错误。谁能帮助我了解我的代码有什么问题?如果还有任何问题,请发表评论,我会尽力回答。

1 个答案:

答案 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个时期,它似乎训练得不太好。