我有一个回归tf.keras.Model
,它包含:
x: tuple[np.ndarray, np.ndarray]
,其中两个项目具有不同的形体- 形状为
(128, 1152)
和(1, 256)
y: float
我把我的模型和训练整理成这样:
class MyModel(tf.keras.Model):
def __init__(self):
... # Omitted for brevity
def call(self, inputs: tuple[tf.Tensor, tf.Tensor], training=None, mask=None):
# Unpacks the two-tuple
weights_1, weights_2 = inputs
... # Omitted for brevity
# NOTE: item 0's shape is (128, 1152), item 1's shape is (1, 256)
datapoint_x: tuple[np.ndarray, np.ndarray]
datapoint_y: float
model = MyModel()
model(inputs=datapoint_x) # Works fine
然而,当我转到fit
模型时,我得到一个Exception
:
>>> model.fit(x=datapoint_x, y=np.array(datapoint_y))
Traceback (most recent call last):
File "/path/to/python3.10/site-packages/IPython/core/interactiveshell.py", line 3433, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-a5dfb3dd4846>", line 1, in <module>
model.fit(x=datapoint_x, y=np.array(datapoint_y))
File "/path/to/python3.10/site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/path/to/python3.10/site-packages/tensorflow/python/framework/tensor_shape.py", line 910, in __getitem__
return self._dims[key]
IndexError: tuple index out of range
我研究了这个,self._dims
是()
,key
是0
。
在一个有两个元组x的数据集上,调用Model.fit
的正确方法是什么?
1条答案
按热度按时间r6l8ljro1#
答案是
Model.fit
正在对x和y进行迭代,所以我必须在x[0]
和y
之前添加一个批处理维。这可以使用
np.newaxis
或np.expand_dims
轻松完成。