如何使张量流模型将列表作为输入?

时间:2020-08-25 21:24:58

标签: python python-3.x tensorflow google-colaboratory

我是tensorflow的新手,我正在制作一个可以进行乘法运算的AI,
我需要使它能够使我的模型将列表作为输入。

这是我的代码:

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]))

,这是错误消息:

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]

2 个答案:

答案 0 :(得分:0)

要解决您的问题,您应该做三件事:

1-像这样将dtype中的multiplication_qobject更改为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-在模型的第一个密集层中,使用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-对于预测函数,您应该传递list的{​​{1}}而不是list,因为您使用{{1}的list进行了训练}

list

答案 1 :(得分:-1)

尝试将第一密集层中的输入设置为multiplication_q.shape,将输入形状设置为1时将输入形状设置为32, 2

编辑:下面的代码解决了您的问题,尽管由于它不是很准确,所以您必须尝试一些东西。

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)))