# 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 = []

# y_train = []

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

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

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

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

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

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)
进一步向下：

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

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

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

2019-07-17 23:25:43
赞同 展开评论 打赏

714
0
0
124
2
0
46
1
0
13
1
0
32
1
0
23
1
0
127
2
0
74
2
0
49
1
0
30
1
0