tensorflow基本常用操作

根据之前学习的基础之后,可以利用所学知识简单的做一个循环操作。

递增输出0到3:

 

 

 

import numpy as np
from numpy import float32
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
a=3
w = tf.zeros([3,4],float32)
x = tf.range(3,18,3)
y=tf.zeros_like(x)
h=tf.ones([1,2],float32)
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    sess.run(init)
    print(w.eval())
    print(x.eval())
    print(y.eval())
    print(h.eval())

还学习到了tensorflow的简单定义矩阵变量的方法:

import numpy as np
from numpy import float32
tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
a=3
w = tf.zeros([3,4],float32)#创建3行4列,值均为0的矩阵
x = tf.range(3,18,3)#从3到18间隔为3的矩阵序列(不包含18)
y=tf.zeros_like(x)#创建与x同列同行的值为0的矩阵
h=tf.ones([1,2],float32)#创建1行2列,值均为1的矩阵
init = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as sess:
    sess.run(init)
    print(w.eval())
    print(x.eval())
    print(y.eval())
    print(h.eval())

 

 在tensorflow中定义变量类型时由于兼容性最好使用float32,因为无论gpu还是cpu都可以使用flaot32,减少错误的出现

posted @ 2021-01-12 22:21  doublebest1  阅读(81)  评论(0编辑  收藏  举报