[英]Keras : problem with gradients, custom layer won't work in a sequential model
[英]Cardinality problem with keras Sequential model
我对 keras 有一些问题。 我想预测一个序列中的下一个数字,我决定使用 keras.Sequential 来做到这一点(因为我们的教授谈到了递归神经网络)。 在 model.fit 运行结束时,代码警告说测试集的基数存在问题。 我是 keras(和一般编码)的新手,所以我看不出问题出在哪里,所以我需要你的帮助。 这是我的代码:
# here there's some code to construct the Fibonacci sequence mod 15 in a dataset (my code has to be generic)
predicted_values=[1,2,3,4,5]
rows=df_fib.shape[0]
# Dictionary for storing generated models
models = {}
#I want to predict k values in the sequence
for k in predicted_values:
n = int(rows*(2/3))
X_train = df_fib.iloc[0:n, 0].values
y_train = df_fib.iloc[n:n+k, 0].values
X_test = df_fib.iloc[n+k:rows-k, 0].values
y_test = df_fib.iloc[rows-k:, 0].values
model = keras.Sequential()
model.add(layers.Dense(1, activation = 'relu'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train.reshape([1,-1]), y_train, epochs=20)
scores = model.evaluate(X_test.reshape([1,-1]), y_test)
print('Accuracy Score - ',k,' values : ' % scores[1]*100)
警告是:
ValueError: Data cardinality is ambiguous:
x sizes: 1
y sizes: 2
Please provide data which shares the same first dimension.
您的数据形状不匹配。 您的数据的第一个维度,通常是您的批量大小,特别是因为您没有指定一个,是不同的,因此不清楚如何使用数据。 同样在顺序 model 的第一层上,您将要设置数据的 input_shape,例如: model.add(layers.Dense(16, activation='relu', input_shape=(6,))) 除外您的输入显然不会是 6。 玩转 x_train 和 y_train 的 output 形状的打印,这应该可以帮助您找出问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.