Tensorflow lesson 3---变量Variable

Tensorflow中的变量就是一个放在内存中的tensor结构,用于在计算过程中保存数据,变量的数值可以保存到文件中,也可以从文件中读取

1.变量的初始化

import tensorflow as tf

Weights=tf.Variable(tf.random_normal([3,2],stddev=0.35),name="weights")#声明一个Weights的变量

print(Weights)#打印Weights变量结构
init=tf.global_variables_initializer()#初始化变量

with tf.Session() as sess:#执行session任务
    sess.run(init)#初始化认为
    print(sess.run(Weights))#打印Weights的值

tensorflow中的变量必须被初始化,否则其内容是空的,以上代码执行完后会打印出一个3行2列的矩阵,值随机的,执行的输出结果如下

<tf.Variable 'weights:0' shape=(3, 2) dtype=float32_ref>
[[ 0.01990979 -0.26959115]
 [ 0.32198292 -0.09266231]
 [-0.32708889  0.3107968 ]]

2.变量保存到文件

import tensorflow as tf

Weights=tf.Variable(tf.random_normal([3,2],stddev=0.35),name="weights")

print(Weights)

saver = tf.train.Saver()#声明Saver
init=tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(Weights))
    save_path = saver.save(sess, "/Users/william/tmp/model.ckpt")#保存到文件
    print("Model saved in file: %s" % save_path)#打印保存的路径

tensorflow是可以将变量保存到文件的,要用到的是tf.train.Saver,以上这段代码执行完成后,就会在tmp文件夹下身材变量保存的文件
这里写图片描述

3.变量的读取
可以保存到文件,就可以从文件中把变量读取出来

import tensorflow as tf

Weights=tf.Variable(tf.random_normal([3,2],stddev=0.35),name="weights")

print(Weights)

saver = tf.train.Saver()

with tf.Session() as sess:
    saver.restore(sess, "/Users/william/tmp/model.ckpt")
    print("the restore variable Weights= %s" % sess.run(Weights))

以上代码把变量读取出来,并打印出来,其输出如下:

<tf.Variable 'weights:0' shape=(3, 2) dtype=float32_ref>
the restore variable Weights= [[ 0.01990979 -0.26959115]
 [ 0.32198292 -0.09266231]
 [-0.32708889  0.3107968 ]]

如果变量是从文件中读取出来,就不需要初始化,只需要声明就可以了

tao@thp:~/文档/PyTorch_Practice/lesson2/rmb_classification$ tensorboard --logdir=lesson5/runs 2025-03-25 19:57:40.459192: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-03-25 19:57:40.459794: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used. 2025-03-25 19:57:40.461640: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used. 2025-03-25 19:57:40.466451: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered WARNING: All log messages before absl::InitializeLog() is called are written to STDERR E0000 00:00:1742903860.474475 9404 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E0000 00:00:1742903860.476797 9404 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered W0000 00:00:1742903860.483264 9404 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1742903860.483289 9404 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1742903860.483292 9404 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1742903860.483294 9404 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. 2025-03-25 19:57:40.485439: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI AVX512_BF16 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. E0000 00:00:1742903861.655321 9404 cuda_executor.cc:1228] INTERNAL: CUDA Runtime error: Failed call to cudaGetRuntimeVersion: Error loading CUDA libraries. GPU will not be used.: Error loading CUDA libraries. GPU will not be used. W0000 00:00:1742903861.664941 9404 gpu_device.cc:2341] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform. Skipping registering GPU devices... NOTE: Using experimental fast data loading logic. To disable, pass "--load_fast=false" and report issues on GitHub. More details: https://github.com/tensorflow/tensorboard/issues/4784 Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all TensorBoard 2.19.0 at http://localhost:6006/ (Press CTRL+C to quit) 这里输入对应的网址打不开
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DiegoRobot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值