'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 = []
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)]]
我设法让您的代码处理一些更改,由于您使用了“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个时代进行良好的训练)。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。