[英]Issues when converting tensorflow/keras model to tensorflow lite model
[英]keras/tensorflow model issues
我正在生成一个预测机器 state 的 ML model。 该机器遵循以下等式 A x + B y = x_next,其中 x、y 和 x_next 是 1x4 向量。 所以我的 ML model 将 x 和 y 作为输入和输出 x_next。 但是我在训练中遇到了问题。
data_chosen = random.sample(state_input_next_state_list, int(len(state_input_next_state_list)* 0.8 ))
#x_train = list(map(lambda x: np.array(x[0] + x[1]), data_chosen))
x_data = list(map(lambda x: np.vstack((x[0], x[1])) , data_chosen))
y_data = list(map(lambda x: np.array(x[2]), data_chosen))
print(x_data[0])
print(x_data[0].shape)
print(y_data[0])
print(type(x_data[0]))
# [[-0.10094348 -0.96692593 1.16288356 -1.39277914]
# [ 0. 0.00338941 0. -0.00338941]]
# (2, 4)
# [-0.11705892 -0.97656013 1.13967058 -1.37345424]
# <class 'numpy.ndarray'>
注意 print 语句的结果。
model = Sequential()
model.add(Dense(max(x_data[0].shape), input_shape=x_data[0].shape, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(y_data[0]), activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
model.fit(x_data, y_data,
epochs=20,
batch_size=len(y_data))
我得到的错误是
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-105-f8499d1a6973> in <module>
1 model.fit(x_data, y_data,
2 epochs=20,
----> 3 batch_size=len(y_data))
c:\software\python37\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1152 sample_weight=sample_weight,
1153 class_weight=class_weight,
-> 1154 batch_size=batch_size)
1155
1156 # Prepare validation data.
c:\software\python37\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
577 feed_input_shapes,
578 check_batch_axis=False, # Don't enforce the batch size.
--> 579 exception_prefix='input')
580
581 if y is not None:
c:\software\python37\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
107 'Expected to see ' + str(len(names)) + ' array(s), '
108 'but instead got the following list of ' +
--> 109 str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
110 elif len(names) > 1:
111 raise ValueError(
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 3 arrays: [array([[-0.19053051, -0.38436736, 0.35624974, -0.04435445],
[ 0. , 0.00178595, 0. , -0.00178595]]), array([[-3.92162966e-01, 6.14237515e-01, -6.34753706e-01,
1.04811...
我找到了解决问题的方法
n = 100
states = [random_state() for _ in range(n) ]
inputs = [random_input() for _ in range(n) ]
state_input_product = list(itertools.product(states,inputs))
print(len(state_input_product))
#next_states = list(map(next_state, state_input_product))
state_input_next_state_list = list(map(bundle_state_input_next_state, state_input_product))
print(state_input_next_state_list[0])
# In[82]:
train_chosen = random.sample(state_input_next_state_list, int(len(state_input_next_state_list)* 0.8 ))
test_chosen = random.sample(state_input_next_state_list, int(len(state_input_next_state_list)* 0.2 ))
x_train = np.stack(list(map(lambda x: np.concatenate((x[0], x[1])) , train_chosen)))
y_train = np.stack(list(map(lambda x: np.array(x[2]), train_chosen)))
x_test = np.stack(list(map(lambda x: np.concatenate((x[0], x[1])) , test_chosen)))
y_test = np.stack(list(map(lambda x: np.array(x[2]), test_chosen)))
# In[93]:
model = Sequential()
model = tf.keras.Sequential(
[
tf.keras.layers.Input(shape=(8), name='INPUT'),
tf.keras.layers.Dense(32, activation='softmax', name='middle'),
tf.keras.layers.Dense(len(y_data[0]), activation='softmax', name='OUTPUT')
])
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.summary()
# In[91]:
batch_size = 100
epochs = 10
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1)
# In[92]:
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])
# In[ ]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.