java调⽤tensorflow训练好的模型1. python的处理
多谢star
⾸先训练⼀个模型,代码如下
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
java调用python模型from tensorflow.python.framework import graph_util
## -1到1之间随机数 100个
train_X = np.linspace(-1, 1, 100)
train_Y = 2*train_X + np.random.randn(*train_X.shape)*0.1
# 显⽰模拟数据点
plt.plot(train_X, train_Y, 'ro', label='test')
plt.legend()
plt.show()
# 创建模型
# 占位符
X = tf.placeholder("float",name='X')
Y = tf.placeholder("float",name='Y')
# 模型参数
# W初始化为-1到1之间的⼀个数字
W = tf.Variable(tf.random_normal([1]), name="weight")
# b初始化为0 也是⼀维定义变量
b = tf.s([1]), name="bias")
# 前向结构 mulpiply两个数相乘
z = tf.multiply(X, W) + b
op = tf.add(tf.multiply(X, W),b,name='results')
# 反向优化
cost = tf.reduce_mean(tf.square(Y - z))
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# 初始化所有变量
init = tf.global_variables_initializer()
# 定义参数
training_epochs = 20
display_step = 2
def moving_avage(a, w=10):
if len(a) < w:
return a[:]
return [val if idx<w else sum(a[(idx-w):idx])/w for idx, val in enumerate(a)]
saver = tf.train.Saver()
# 启动session
with tf.Session() as sess:
sess.run(init)
# 存放批次值和损失值
plotdata = {"batchsize": [], "loss": []}
# 向量模型输⼊数据
# 向量模型输⼊数据
for epoch in range(training_epochs):
for(x, y) in zip(train_X, train_Y):
sess.run(optimizer, {X:x, Y:y})
# 显⽰训练中的详细信息
if epoch % display_step == 0:
loss = sess.run(cost, {X:train_X, Y:train_Y})
print("Epoch:", epoch+1, "cost=", loss, "W=", sess.run(W), "b=",sess.run(b))
if not (loss == "NA"):
plotdata["batchsize"].append(epoch)
plotdata["loss"].append(loss)
print("Finished!")
#保存模型
saver.save(sess, "model/first")
print("cost =", sess.run(cost, feed_dict={X:train_X, Y:train_Y}), "W=", sess.run(W), "b=", sess.run(b)) const_graph = vert_variables_to_constants(sess, aph_def,["results"])
with tf.gfile.FastGFile("model/first.pb",mode='wb') as f:
f.write(const_graph.SerializeToString())
# 图形显⽰
plt.plot(train_X, train_Y, 'ro', label='Original data')
plt.plot(train_X, sess.run(W)*train_X+sess.run(b),label='Filttedline')
plt.legend()
plt.show()
plotdata["avgloss"] = moving_avage(plotdata["loss"])
# plt.figure(1)
plt.subplot(211)
plt.plot(plotdata["batchsize"],plotdata["avgloss"], 'b--')
plt.xlabel('Minibatch number')
plt.ylabel('Loss')
plt.title('Minibatch run vs, Trainging loss')
plt.show()
print("x=0.2, z=", sess.run(z, {X:0.2}))
测试模型:
from tensorflow.python.platform import gfile import tensorflow as tf
sess = tf.Session()
with gfile.FastGFile('model/first.pb','rb') as f:
graph_def = tf.GraphDef()
graph_def.ad())
tf.import_graph_def(graph_def,name='')
sess.run(tf.global_variables_initializer())
print(sess.run('weight:0'))
print(sess.run('bias:0'))
input_x = _tensor_by_name('X:0')
op = _tensor_by_name('results:0')
ret = sess.run(op, feed_dict={input_x: 2})
print(ret)
2 java的处理
新建⼀个maven项⽬
把模型加⼊项⽬中.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论