第一次的tensorflow实践

作者: shaneZhang 分类: 机器学习的实践 发布时间: 2018-12-23 22:02
#coding=utf-8
import tensorflow as tf

print "这是我第一次的tensorflow实践"
#定义一个张量 1.0 2.0
a = tf.constant([1.0,2.0])
#定义一个张量等于 3.0 4.0
b = tf.constant([3.0,4.0])
#实现a加b的加法
result = a + b
#打印a加b的结果
print  result
#定义一个2阶的张量等于[[1.0,2.0]]
x = tf.constant([[1.0,2.0]])
#定义一个2阶的张量等于[[3.0],[4.0]]
w = tf.constant([[3.0],[4.0]])
#实现xw矩阵乘法
y = tf.matmul(x,w)
#打印结果
print  y
#执行会话并打印以上两个计算的结果
with tf.Session() as sess:
    print sess.run(result)
    print sess.run(y)
执行的结果如下:
/Users/zhangyuqing/tensorflow/bin/python /Users/zhangyuqing/PycharmProjects/MyTensorFlow/FirstPython.py
这是我第一次的tensorflow实践
Tensor("add:0", shape=(2,), dtype=float32)
Tensor("MatMul:0", shape=(1, 1), dtype=float32)
2018-12-23 21:58:56.452380: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.2
[4. 6.]
[[11.]]

神经网络中常用的生成随机数/数组的函数有:
tf.random_normal() 生成正态分布随机数
tf.truncated_normal() 生成去掉过大偏离点的正态分布随机数
tf.random_uniform() 生成均匀分布随机数
tf.zeros 表示生成全 0 数组
tf.ones 表示生成全 1 数组
tf.fill 表示生成全定值数组
tf.constant 表示生成直接给定值的数组

本页面支持繁体中文友好显示:第一次的tensorflow实践

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复