我是 Tensorflow 和 Python 的新手,我已经看过网站上的所有教程,现在我正在使用我的第一个真实数据集。
我想要用神经网络做的是在了解日常趋势的情况下预测一些发电厂的能源消耗。我有一个包含所有这些(真实)值的 .xlsx 文件。使用 Pandas,我对训练集和验证集中的数据进行了分割和标准化(即 train_x 和 train_y,其中 train_x 是时间,train_y 是标签)。 x 和 y 数组都是 numpy.ndarray ,格式如下(仅头部):
print(train_x)
[ 644]
[ 645]
[ 646]
打印(train_y)
[-0.09154356 1.10702972 1.13661838]
[ 0.05104414 1.39112378 1.5319337 ]
[-0.05719421 1.40702419 1.48187637]
然后我创建了模型:
model = keras.Sequential([
keras.layers.Dense(64, activation=tf.nn.relu, input_shape= (train_x.shape([0]))),
keras.layers.Dense(3, activation=tf.nn.softmax)])
model.compile(loss='categorical_cross_entropy',
optimizer='Adam',
metrics=['accuracy'])
history = model.fit(train_x, train_y, epochs=5, verbose=1)
但是当我运行脚本时出现此错误:
TypeError: 'tuple' object is not callable
我猜问题出在层的输入形状上,或者可能是损失函数的形状上,如建议的 here ,所以我尝试修改损失函数:
LOSS = tf.nn.categorical_cross_entropy_with_logits(logits=3, labels=3)
当然还有 model.compile:
model.compile(loss=LOSS,
optimizer='Adam',
metrics=['accuracy'])
但我又遇到了同样的错误:
TypeError: 'tuple' object is not callable
我哪里出错了?
最佳答案
它应该是array.shape[0]
,而不是array.shape([0])
。 shape
是 numpy 数组的属性,而不是方法。正确的语法应该是:
keras.layers.Dense(64, activation=tf.nn.relu, input_shape= (train_x.shape[-1],)),
此外,将 train_x
和 train_y
更改为二维数组,形状为 [length_of_array,1]。
关于python - Tensorflow 密集标签形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53351130/