文章目录:
一.tensor张量
二.Session
三.常量和变量
四.placeholder传入值
五.激励函数
六.总结
https://github.com/eastmountyxz/
AI-for-TensorFlow
https://github.com/eastmountyxz/
AI-for-Keras
学Python近八年,认识了很多大佬和朋友,感恩。由于在外求学且需要养娃,故在CSDN设置成了最低价收费专栏,觉得不错的可以购买抬爱;但作者的本意是帮助更多初学者入门,因此在github开源了所有代码,也在公众号同步更新。深知自己很菜,得拼命努力前行,编程也没有什么捷径,干就对了。希望未来能更透彻学习和撰写文章,也能在读博几年里学会真正的独立科研。同时非常感谢参考文献中的大佬们的文章和分享。
- https://blog.csdn.net/eastmount
# 0阶张量 标量
5
# 1阶张量 向量大小为3
[1., 2., 3.]
# 2阶张量 2*3矩阵
[[1., 2., 3.],
[4., 5., 6.]]
# 3阶张量 大小为2*3*2
[[[1., 2.],[3., 4.],[5., 6.]],
[[7., 8.],[9., 10.],[11., 12.]]]
# -*- coding: utf-8 -*-
import tensorflow as tf
#定义变量
a = tf.constant([1, 2, 3], name="a")
b = tf.constant([[1, 2, 3],[4, 5, 6]])
print(a)
print(b)
#创造数组0和1
c = tf.zeros([2, 3])
d = tf.ones([2,3])
print(c)
print(d)
#随机生成一个正态分布
e = tf.random.normal([5,3])
print(e)
在构建阶段,op的执行步骤被描述成一个图
在执行阶段,使用会话执行图中的op
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 30 16:38:31 2019
@author: Eastmount CSDN YXZ
"""
import tensorflow as tf
# 建立两个矩阵
matrix1 = tf.constant([[3,3]]) #常量 1行2列
matrix2 = tf.constant([[2],
[2]]) #常量 2行1列
# 矩阵乘法 matrix multiply 类似于numpy.dot()函数
product = tf.matmul(matrix1, matrix2)
# 两种利用Session会话控制的方法
# 方法一
sess = tf.Session()
output = sess.run(product) # 执行操作 每run一次TensorFlow才会执行操作
print(output)
sess.close()
# 方法二
with tf.Session() as sess: # 打开Session并且赋值为sess 运行结束会自动close
output = sess.run(product)
print(output)
[[12]]
[[12]]# 创建1*2矩阵常量
c1 = tf.constant([[1., 1.]])
# 创建2*1矩阵常量
c2 = tf.constant([[2.],[2.]])
# 创建一个0阶变量并初始化为0
state = tf.Variable(0, name='counter')
# 标准差为0.35的正态分布初始化一个形状(10,20)的变量
w = tf.Variable(tf.random_normal([10, 20], stddev=0.35), name="w")
定义变量一定要初始化,使用:
global_variables_initializer()
initialize_all_variables()
定义Session时一定要使用sess.run(init)初始化,然后才能使用
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 1 16:52:18 2019
@author: Eastmount CSDN YXZ
"""
import tensorflow as tf
# 定义变量 初始值为0 变量名字为counter(用于计数)
state = tf.Variable(0, name='counter')
print(state.name)
print(state)
# 定义常量
one = tf.constant(1)
print(one)
# 新变量
result = tf.add(state, one)
# 更新: result变量加载到state中 state当前变量即为result
update = tf.assign(state, result)
# Tensorflow中需要初始化所有变量才能激活
init = tf.global_variables_initializer() # must have if define variable
# Session
with tf.Session() as sess:
sess.run(init)
# 三次循环更新变量
for _ in range(3):
sess.run(update)
print(sess.run(state)) #直接输出state没用 需要run
1
2
3
import tensorflow as tf
# 定义变量
a = tf.constant([5, 3], name='input_a')
# 计算
b = tf.reduce_prod(a, name='prod_b')
c = tf.reduce_sum(a, name='sum_c')
d = tf.add(b, c, name='add_d')
# Session
with tf.Session() as sess:
print('a:', sess.run(a))
print('b:', sess.run(b))
print('c:', sess.run(c))
print('d:', sess.run(d))
a: [5 3]
b: 15
c: 8
d: 23
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 1 18:21:29 2019
@author: Eastmount CSDN YXZ
"""
import tensorflow as tf
# 传入值 给定type
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
# 输出 乘法运算
output = tf.multiply(input1, input2)
# Session
with tf.Session() as sess:
# placeholder需要传入值,在session.run时传入字典类型
print(sess.run(output, feed_dict={input1:[7.], input2:[2.0]}))
[14.]
import tensorflow as tf
a = tf.constant([-1.0, 2.0])
# 激励函数
with tf.Session() as sess:
b = tf.nn.relu(a)
print(sess.run(b))
c = tf.sigmoid(a)
print(sess.run(c))
[0. 2.]
[0.26894143 0.880797 ]
天行健,君子以自强不息。
地势坤,君子以厚德载物。
[1] 神经网络和机器学习基础入门分享 - 作者的文章
[2] 斯坦福机器学习视频NG教授:
https://class.coursera.org/ml/class/index
[3] 书籍《游戏开发中的人工智能》
[4] 网易云莫烦老师视频(强推):
https://study.163.com/course/courseLearn.htm?courseId=1003209007
[5] 神经网络激励函数 - deeplearning
[6] tensorflow架构 - NoMorningstar
[7] 《TensorFlow2.0》低阶 api 入门 - GumKey
[8]TensorFlow之基础知识 - kk123k
[9] Tensorflow基础知识梳理- sinat_36190649
[10] 深度学习(二):TensorFlow 基础知识 - 希希里之海
[11] tensorflow基础概念 - lusic01
[12] tensorflow:激活函数(Activation Function) - haoji007
[13] AI => Tensorflow2.0语法 - 张量&基本函数(一)