model = keras.Sequential([keras.layers.Dense(1, input_shape=(1, 4))])
model.compile(optimizer='sgd', loss='mean_squared_error')
#
xs = np.array([[[-1.0, 1.0, -1.0, 1.0]],
[[0.0, 2.0, 0.0, 2.0]],
[[1.0, 3.0, 1.0, 3.0]],
[[2.0, 4.0, 2.0, 4.0]],
[[3.0, 5.0, 3.0, 5.0]],
[[4.0, 6.0, 4.0, 6.0]]], dtype=np.float32)
ys = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=np.float32)
#
model.fit(xs, ys, epochs=500)
#
# # Write out to keras save file
keras_file = 'linear.h5'
# keras.models.save_model(model, keras_file)
# input to predict
model_input = np.array([[[8.0, 10.0, 8.0, 10.0]]], dtype=np.float32)
# make predictions from the h5 file
h5_model = keras.models.load_model(keras_file)
h5_prediction = h5_model.predict(model_input)
# h5_prediction will always be around 19, which is correct
print('Prediction from h5 model: {}'.format(h5_prediction))
# Convert the keras file to TensorFlow Lite
converter = lite.TocoConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open('model_test.tflite', 'wb').write(tflite_model)
有错误:
tensorflow / lite / toco / tooling_util.cc:625]检查失败:暗>> 1(0对1)
或
I tensorflow / lite / toco / graph_transformations / graph_transformations.cc:39]在一般图形转换之前:17个运算符,31个数组(0个量化)。