模型 windows训练 linux部署,Tensorflow模型保存与加载

本文介绍了如何在Windows环境下训练TensorFlow模型并将其部署到Linux环境中。首先,详细阐述了模型的保存过程,强调了在定义变量和操作时使用name属性的重要性。接着,展示了模型的加载步骤,包括如何利用get_tensor_by_name恢复模型的变量和结构。最后,通过两个代码实例演示了模型的保存和加载,并提到了ckpt文件在跨平台部署时的局限性,建议将模型保存为pb文件以实现更好的迁移性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.保存模型

所谓的模型保存,也就是冻结(freeze)模型,将该模型的图结构和该模型的权重固化到一起。

二.加载模型

在恢复模型的时候,通过get_tensor_by_name获得模型中的变量,然后对变量进行赋值。

三.代码实例

Demo1:单纯地训练,不生成模型

#-*- coding:utf-8 -*-

import tensorflow as tf

import numpy as np

with tf.variable_scope('Placeholder'):

inputs_placeholder = tf.placeholder(tf.float32,name = 'inputs_placeholder',shape = [None,10])

labels_placeholder = tf.placeholder(tf.float32,name = 'labels_placeholder',shape = [None,1])

with tf.variable_scope('NN'):

W1 = tf.get_variable('W1',shape = [10,1],initializer = tf.random_normal_initializer(stddev = 1e-1))

b1 = tf.get_variable('b1',shape = [1],initializer = tf.constant_initializer(0.1))

W2 = tf.get_variable('W2',shape = [10,1],initializer = tf.random_normal_initializer(stddev = 1e-1))

b2 = tf.get_variable('b2',shape = [1],initializer = tf.constant_initializer(0.1))

a1 = tf.nn.relu(tf.matmul(inputs_placeholder,W1) + b1)

a2 = tf.nn.relu(tf.matmul(inputs_placeholder,W2) + b2)

y = tf.div(tf.add(a1,a2),2)

with tf.variable_scope('Loss'):

loss = tf.red

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值