Tensorflow学习1

Tensorflow学习1

之前简单使用过tensorflow,但比较少,记忆也很模糊;需要重新慢下来学习一下。我是一个不常记笔记的人,但最近在家实在太过闲以至于焦虑+之前的学习自我感觉太看重进度缺少练习,效果并不好,所以最近会尝试做一些记录。
作为入门参考教程如下:https://github.com/machinelearningmindset/TensorFlow-Course

0-Warm up

Warm up 内容很简单,代码及注释如下,基本来自教程。

from __future__ import print_function
import tensorflow.compat.v1 as tf
import os

# The default path for saving event files is the same folder of this python file.
tf.app.flags.DEFINE_string(
        'mylog_dir', os.path.dirname(os.path.abspath(__file__)) + '/logs',
        'Directory where event logs are written to.') #定义一个用于接收 string 类型数值的变量;函数带3个参数,分别是变量名称,默认值,用法描述

# Store all elements in FLAG structure
FLAGS = tf.app.flags.FLAGS #The tf.app.flags.FLAGS points to all defined flags using the FLAGS indicator. From now on the flags can be called using FLAGS.flag_name

# The user is prompted to input an absolute path.
# os.path.expanduser is leveraged to transform '~' sign to the corresponding path indicator
#       Example: '~/logs' aquals to '/home/username/logs'
if not os.path.isabs(os.path.expanduser(FLAGS.mylog_dir)):
    raise ValueError('You must assign absolute path for --log_dir')
tf.disable_eager_execution()    
# Define some sentence
welcome = tf.constant('Welcome to TensorFlow world!')

# Run the session
with tf.Session() as sess:
    
    writer = tf.summary.FileWriter(os.path.expanduser(FLAGS.mylog_dir), sess.graph) # write the summaries into event files
    print("output:", sess.run(welcome))
    
# Closing the writer.
writer.close()
sess.close()

补充
(1)FLAGS可以自动接收到python脚本运行时的命令行参数,并可将其作为定义的全局变量引用(但目前还是没懂有什么用)。
(2)因为版本问题遇到报错1:module ‘tensorflow’ has no attribute ‘app’,是tensorflow版本问题。
参考https://blog.csdn.net/qq_24163555/article/details/102954704解决。
(3)因为版本问题遇到报错2:The Session graph is empty. Add operations to the graph before calling run().
tf.compat.v1.disable_eager_execution();注意位置要在定义tf.run()的内容前,在这里是welcome
(4)The flag ‘log_dir’ is defined twice.
a. 修改了名字
b.注意重启kernel
总结
简单接触了两个函数,相当于一点复习;之后要把版本差异时刻放在心间。
C’est tout.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值