tensorflow 线性模型保存为pb格式,并且在tfjs中使用

本文介绍了如何将TensorFlow的线性模型保存为.pb格式,并详细展示了如何利用tensorflowjs_converter转换模型以便在tfjs中使用。通过示例代码,展示了模型的训练、加载以及在Vue应用中的实现。
摘要由CSDN通过智能技术生成

原文链接: tensorflow 线性模型保存为pb格式,并且在tfjs中使用

上一篇: tensorflowjs 简单使用

下一篇: tfjs mnist 手写数字识别web版

保存时需要指定,output_node_names 是准备保存的模型节点名称的列表,只会保存指定节点的数据

@tf_export("graph_util.convert_variables_to_constants")
def convert_variables_to_constants(sess,
                                   input_graph_def,
                                   output_node_names,
                                   variable_names_whitelist=None,
                                   variable_names_blacklist=None):    


constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph_def, ['output'])
with tf.gfile.FastGFile(SAVE_PATH, mode='wb') as f:
    f.write(constant_graph.SerializeToString())

用于拟合直线y=2x-1的模型代码

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import os
from tensorflow.python.framework import graph_util


# 模拟生成数据点, 返回np数组
def get_data(batch_size):
    x = np.random.uniform(-10, 10, (batch_size, 1))
    y = 2 * x - 1 + np.random.randn(batch_size, 1)
    return x, y


SAVE_DIR
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值