TensorFlow 第一步 开门见山:Hello World!

TensorFlow最重要的数据类型 张量Tensor,可用numpy的array传入tf.Variable(),构建带初值的张量;也可用tf.convert_to_tensor()

# coding=utf-8

import os  
#os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息  
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error   
#os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error  

import tensorflow as tf
import numpy as np
hello=tf.constant("Hello World!")
tensor_2d=np.array([(1,2),(2,2),(3,0)])
tensor_2d=tf.Variable(tensor_2d)
print(hello,tensor_2d)
'''
Tensor("Const:0", shape=(), dtype=string) <tf.Variable 'Variable:0' shape=(3, 2) dtype=int32_ref>
'''
with tf.Session() as sess:
    print(sess.run(hello))
    tensor_1d=tf.convert_to_tensor(6, dtype=tf.float64)
    print(sess.run(tensor_1d))
    sess.run(tf.global_variables_initializer())
    print(sess.run(tensor_2d)) 
'''
b'Hello World!'
6.0
[[1 2]
 [2 2]
 [3 0]]
'''

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值