[英]How can I make a tensorflow model take lists as input?
I'm new with tensorflow, and I'm making an AI that does multiplication,我是 tensorflow 的新手,我正在制作一个可以进行乘法运算的 AI,
and I need to make it so that my model can take lists as input.我需要这样做,以便我的模型可以将列表作为输入。
Here is my code:这是我的代码:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
multiplication_q = np.array([[10,10],[1,1],[2,2],[0,0],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[1,0],[11,10],[27,0],[30,2],[4,3],[17,22],[20,0],[8,13],[21,4],[19,24],[11,19],[8,2],[4,5],[11,11],[1,15],[2,12],[15,3],[18,0],[49,7],[5,7],[12,4]], dtype=object)
multiplication_a = np.array([100,1,4,0,9,16,25,36,49,64,96,0,110,0,60,12,374,0,104,84,456,209,16,20,121,15,24,45,0,343,35,48], dtype=float)
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=4, input_shape=[1]),
tf.keras.layers.Dense(units=4),
tf.keras.layers.Dense(units=1)
])
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(multiplication_q, multiplication_a, epochs=750, verbose=False)
print(model.predict([4, 5]))
and here is the error message:这是错误消息:
ValueError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:747 train_step
y_pred = self(x, training=True)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
' but received input with shape ' + str(shape))
ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [32, 2]
To fix your issue you should do 3 things:要解决您的问题,您应该做 3 件事:
1- Change the dtype
in the multiplication_q
from object
to int
like this: 1-将multiplication_q
的dtype
从object
更改为int
如下所示:
multiplication_q = np.array([[10,10],[1,1],[2,2],[0,0],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[1,0],[11,10],[27,0],[30,2],[4,3],[17,22],[20,0],[8,13],[21,4],[19,24],[11,19],[8,2],[4,5],[11,11],[1,15],[2,12],[15,3],[18,0],[49,7],[5,7],[12,4]], dtype=int)
2- And in the first Dense layer of your model use input_shape=(2,)
instead of input_shape=[1]
, like this: 2- 在模型的第一个 Dense 层中使用input_shape=(2,)
而不是input_shape=[1]
,如下所示:
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=4, input_shape=(2,)),
tf.keras.layers.Dense(units=4),
tf.keras.layers.Dense(units=1)
])
3- And for the predict function you should pass a list
of list
and not a list
, cause you did a training with list
of list
3-和用于预测功能,您应该通过一个list
的list
,而不是一个list
,因为你做了一个培训list
的list
model.predict([[4, 5]])
Try setting your input in the first dense layer to multiplication_q.shape
, you set your input shape to be 1
while your input is shaped 32, 2
尝试将第一个密集层中的输入设置为multiplication_q.shape
,将输入形状设置为1
而输入形状为32, 2
EDIT: The code below resolved your issue although you will have to play around with stuff because it is not very accurate.编辑:下面的代码解决了您的问题,尽管您必须玩弄东西,因为它不是很准确。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
multiplication_q = np.asarray([[10,10],[1,1],[2,2],[0,0],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[1,0],[11,10],[27,0],[30,2],[4,3],[17,22],[20,0],[8,13],[21,4],[19,24],[11,19],[8,2],[4,5],[11,11],[1,15],[2,12],[15,3],[18,0],[49,7],[5,7],[12,4]])
multiplication_a = np.asarray([100,1,4,0,9,16,25,36,49,64,96,0,110,0,60,12,374,0,104,84,456,209,16,20,121,15,24,45,0,343,35,48])
multiplication_q = multiplication_q/np.amax(multiplication_q)
multiplication_a = multiplication_a/np.amax(multiplication_a)
model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(2, )))
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(units=1))
model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))
history = model.fit(multiplication_q, multiplication_a, epochs=750)
print(model.predict(np.asarray([[4, 5]])/np.amax(multiplication_q)*np.amax(multiplication_a)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.