我在TensorFlow中学习机器学习是新手。我生成并导出到.csv文件的数据集在这里:tftest.csv。
“distributions”列对应于我试图在SageMath中简化为一系列数字的独特方程系统。“probs”列对应于是否应该根据它所在的行和列,将给定的方程乘以方程的一个给定单项式。以上只是概述,与我的实际问题无关。
无论如何,这是我的代码。我已经尽我所能用注释解释它了。
import csvimport numpy as npimport matplotlib.pyplot as pltimport tensorflow as tfimport tensorflow.keras as kerasdistribution_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 fx_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)]]
我在网上搜索了这个错误,但我似乎无法理解为什么会出现这种情况。谁能帮我理解我的代码有什么问题吗?如果有任何问题,请留下评论,我会尽力回答。
回答:
我通过一些更改成功运行了你的代码,看来错误是由于你使用了“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个epochs,似乎训练效果不佳)。