我有一个具有102个特征的输入数据集,并具有一个对应的二进制输出。根据102个功能,输出为0或1。
输入:
tf.Tensor(
[-1.72999993e-01 -8.20000023e-02 3.38000000e-01 1.35000005e-01
...
0.00000000e+00 2.00000009e-03], shape=(102,), dtype=float64)
输出:
tf.Tensor([1], shape=(1,), dtype=int32)
我正在尝试遵循custom training tutorial并按照以下方式创建此模型:
train_dataset = tf.data.Dataset.from_tensor_slices((train_x,tf.dtypes.cast(label_x, tf.int32)))
features, labels = next(iter(train_dataset))
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(102,)), # input shape required
tf.keras.layers.Dense(10, activation=tf.nn.relu),
tf.keras.layers.Dense(1)
])
predictions = model(features)
但是,当我尝试运行它时出现错误:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-12-d7be7f733930> in <module>()
6 ])
7
----> 8 predictions = model(features)
7 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: In[0] is not a matrix. Instead it has shape [102] [Op:MatMul]
答案 0 :(得分:0)
您可以调整batch
以创建数据集,也可以调整模型中的input_shape
以适应尺寸。
train_x = np.arange(100, dtype=np.int32)
label_x = np.arange(100, dtype=np.int32)
train_dataset = tf.data.Dataset.from_tensor_slices((train_x, label_x)).batch(10)
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(1,)), # input shape required
tf.keras.layers.Dense(10, activation=tf.nn.relu),
tf.keras.layers.Dense(1)
])
for features, labels in train_dataset:
pred = model(features[..., tf.newaxis])
print(pred)
#tf.Tensor(
#[[-21.829016]
# [-22.071556]
# [-22.314102]
# [-22.556648]
# [-22.799194]
# [-23.041737]
# [-23.284283]
# [-23.52683 ]
# [-23.76937 ]
# [-24.011917]], shape=(10, 1), dtype=float32)