我是机器学习的新手。我正在尝试在Tensorflow 2.0中创建一个简单的RNN,但遇到了麻烦。我将其简化为一个重现该问题的最小示例。这个最小示例的目的是让RNN学习重复输出1.0。
import os
import sys
import math
from random import shuffle
import numpy as np
import tensorflow as tf
from time import time as time
epochs = 200
batch_size = 32
chunk_length = 64
features = 10
def main():
train_dataset = np.zeros([batch_size, chunk_length, features]) + 1
test_dataset = np.zeros([batch_size, chunk_length, features]) + 1
with tf.device('/gpu:0'):
model = tf.keras.Sequential([
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='mean_absolute_error',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(train_dataset, batch_size=batch_size, epochs=epochs)
test_loss, test_acc = model.evaluate(test_dataset)
print('Test Loss: {}'.format(test_loss))
print('Test Accuracy: {}'.format(test_acc))
if __name__ == '__main__':
main()
运行此命令时,我得到ValueError: Arguments and signature arguments do not match: 56 57
。如果我注释掉最后一层,则得到ValueError: Arguments and signature arguments do not match: 50 51
。如果我注释掉最后两层,则会得到ValueError: Arguments and signature arguments do not match: 44 45
。
我尝试修改我提供的所有常量(epochs,batch_size,chunk_length和features),但这些常量对错误没有影响。我还尝试了将1逐元素加到numpy数组中,但这也没有效果。
这是TensorFlow中的bug还是我做蠢事?
答案 0 :(得分:3)
我使用的是Tensorflow版本1.13.1,没有GPU,但是希望这仍然可以解决问题。 似乎您只在输入网络输入数据(x),而没有在输入响应数据(y)。因此,该模型没有什么要学习的。我只添加了响应数据train_Y_dataset和test_Y_dataset。以下代码在tensorflow 1.13.1中对我有用,请参见注释以获取更改:
import os
import sys
import math
from random import shuffle
import numpy as np
import tensorflow as tf
from time import time as time
epochs = 200
batch_size = 32
chunk_length = 64
features = 10
def main():
train_X_dataset = np.zeros([batch_size, chunk_length, features]) + 1
# add train_Y_dataset:
train_Y_dataset = np.zeros([batch_size, 1]) + 1
test_X_dataset = np.zeros([batch_size, chunk_length, features]) + 1
# add test_Y_dataset:
test_Y_dataset = np.zeros([batch_size, 1]) + 1 #1
#with tf.device('/gpu:0'):
model = tf.keras.Sequential([
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(
64, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='mean_absolute_error',
optimizer='adam',
metrics=['accuracy'])
# add the response variable train_Y_dataset in fit
history = model.fit(x=train_X_dataset, y=train_Y_dataset, batch_size=batch_size, epochs=epochs)
# add the response variable test_Y_dataset in evaluate
test_loss, test_acc = model.evaluate(x=test_X_dataset, y=test_Y_dataset)
print('Test Loss: {}'.format(test_loss))
print('Test Accuracy: {}'.format(test_acc))
if __name__ == '__main__':
main()
答案 1 :(得分:0)
请记住,model.fit()
和model.evaluate()
之类的功能需要传递标签。