python - 如何修复 ValueError:数据基数不明确,同时拟合模型
问题描述
所以,我试图重新创建sketch-rnn 项目,为你完成一个涂鸦。但我仍然是 tensorflow 和 keras 的新手,不太了解事情是如何工作的。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pickle, copy
with open("cake_100.pickle", "rb") as f:
drawings = pickle.load(f)
# each drawing will be a list of vectors, i.e [x,y,p1,p2,p3]
# p1 means pendown, p2 is penup, and p3 is done.
for i in range(len(drawings)):
drawings[i] = np.expand_dims(drawings[i], 2) # expand dims, cuz thats what the model wants :/
def build_model():
visible = keras.layers.Input(
shape=(5, 1)
) # take one drawing at a time with shape (None,5,1)
# how to handle all these states??? - done
encoder = keras.layers.Bidirectional(
keras.layers.LSTM(
128, activation="tanh", return_state=True, return_sequences=True
)
) # encode sequences into fixed length vectors of shape (5,256), i.e for each sequence in a drawing, for each scalar in that sequence - convert it to a 256 length vector
(
encoder_out,
forward_state_h,
forward_state_c,
backward_state_h,
backward_state_c,
) = encoder(visible)
state_h, state_c = (
forward_state_h + backward_state_h,
forward_state_c + backward_state_c,
)
decoder1 = keras.layers.LSTM(128, activation="tanh", return_sequences=True,)
decoder1_out = decoder1(encoder_out, initial_state=[state_h, state_c],)
decoder2 = keras.layers.Dense(1, activation="relu")
decoder_out = decoder2(decoder1_out)
model = keras.Model(inputs=visible, outputs=decoder_out)
return model
def get_x_and_y(drawings):
X, y = [], []
for drawing in drawings:
# y is X shifted to the right, cuz the model predicts one step ahead in the future
X.append(drawing[:-1]) # get all the values except the last one
y.append(drawing[1:]) # get all the value except the first one
return X, y
model = build_model()
model.summary(100)
model.compile(
optimizer="adam", loss=keras.losses.CategoricalCrossentropy(), metrics=["accuracy"]
)
X_train, y_train = get_x_and_y(drawings)
# X_train[0].shape = y_train[0].shape = (49, 5, 1)
# X_train[1].shape = y_train[1].shape = (30, 5, 1)
# ...
model.fit(X_train, y_train, batch_size=1, epochs=3)
但这提出了一个ValueError: Data cardinality is ambiguous
:
ValueError: Data cardinality is ambiguous:
x sizes: 49, 30, 40, 53, 73, 45, 35, 58, 28, 59, 40, 34, 36, 26, 49, 35, 38, 37, 33, 47, 61, 38, 56, 44, 25, 38, 41, 32, 67, 25, 28, 20, 35, 35, 48, 34, 42, 43, 34, 88, 38, 41, 32, 55, 26, 47, 39, 50, 108, 27, 49, 35, 18, 74, 43, 51, 36, 49, 41, 42, 44, 76, 40, 30, 41, 31, 43, 18, 24, 89, 25, 67, 43, 44, 25, 58, 25, 47, 83, 39, 56, 56, 48, 25, 25, 43, 46, 35, 75, 49, 51, 33, 24, 37, 42, 35, 25, 29, 63, 36
y sizes: 49, 30, 40, 53, 73, 45, 35, 58, 28, 59, 40, 34, 36, 26, 49, 35, 38, 37, 33, 47, 61, 38, 56, 44, 25, 38, 41, 32, 67, 25, 28, 20, 35, 35, 48, 34, 42, 43, 34, 88, 38, 41, 32, 55, 26, 47, 39, 50, 108, 27, 49, 35, 18, 74, 43, 51, 36, 49, 41, 42, 44, 76, 40, 30, 41, 31, 43, 18, 24, 89, 25, 67, 43, 44, 25, 58, 25, 47, 83, 39, 56, 56, 48, 25, 25, 43, 46, 35, 75, 49, 51, 33, 24, 37, 42, 35, 25, 29, 63, 36
Please provide data which shares the same first dimension.
对实际代码本身的任何帮助或建议,非常感谢。提前致谢。
解决方案
推荐阅读
- python - 24x7 运行 Python 应用程序 - 设计问题
- python-3.x - 在python中编写循环函数的正确方法
- flutter - 为什么我的 _onCameraMove 下的函数运行得这么差?
- python - 使用 Python 3 中的 Requests 模块发布请求未显示正确内容
- python - 一个类的 __init__ 在另一个类中做了什么?
- python - 在 Python 中返回(副本)具有给定 id 的二维列表行的最有效(最快)方法是什么?
- testing - 如何跳过 Robot Framework 中的一些可选参数?
- mysql - MYSQL 查询基于另一个表获取 SUM
- javascript - 在 HTML 中显示凭据的安全隐患
- google-bigquery - 计划查询停止写入 BigQuery 中的表