tensorflow学习(1)

莫凡2017tensorflow(使用更简便版)https://github.com/MorvanZhou/Tensorflow-Tutorial

参考莫凡课程:

1.什么是tensorflow

tensorflow是google开发的一款神经网络的Python外部的结构包,也是一个采用数据流图来进行计算的开源软件库,(学习tensorflow可快速入门神经网络)

(再学习神经网络的构造、如何学习神经网络https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/2-1-NN/

(有了tensorflow,可以轻松自如玩转神经网络)

tensorflow官网:https://www.tensorflow.org/

https://tensorflow.google.cn/

版本的函数新定义参见https://tensorflow.google.cn/versions/r1.12/api_docs/python/tf/losses/log_loss

                                       https://github.com/MorvanZhou/Tensorflow-Tutorial

2.为什么使用tensorflow

TensorFlow 无可厚非地能被认定为 神经网络中最好用的库之一. 它擅长的任务就是训练深度神经网络.通过使用TensorFlow我们就可以快速的入门神经网络, 大大降低了深度学习(也就是深度神经网络)的开发成本和开发难度. TensorFlow 的开源性, 让所有人都能使用并且维护, 巩固它. 使它能迅速更新, 提升.(摘自莫凡文章)

3.tensorflow的安装

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/1-2-install/

4.tensorflow小例子

机器学习:模拟我们数据需要的曲线

5.tensorflow如何处理数据结构

结构:是指tensorflow底层的结构,也就是神经网络的结构

输入层、隐藏层(权重weights和偏置biases)、激励函数、梯度处理、对每一层的参数进行进一步的完善和提升、(不断循环)

tensorflow中第一步:建立结构,再把数据放到结构中,让tensorflow自己去运行(tensorflow的另一个理解:向量在建立的结构下飞)

(Tensorflow 首先要定义神经网络的结构, 然后再把数据放入结构当中去运算和 training.)

6.代码实现创建结构、处理结构

(输出每次训练之后的参数)

import tensorflow as tf
import numpy as np

#创建数据x
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data*0.1 + 0.3

#开始创建tensorflow结构
Weights =tf.Variable(tf.random_uniform([1],-1.0,1.0)) #随机变量,用随机数列生成的方式生成,一维数列,范围-1到1
biases = tf.Variable(tf.zeros([1]))  #初始值是0
#学习:从初始值提升到参数训练值

y=Weights*x_data + biases  #预测的y值
loss = tf.reduce_mean(tf.square(y-y_data)) #计算预测的y和实际的y_data的差别
optimizer = tf.train.GradientDescentOptimizer(0.5)  #神经网络对其进行优化 优化器  GradientDescentOptimizer:梯度下降优化器 0.5:学习效率,一般是小于1的数
train= optimizer.minimize(loss)  #用优化器减小误差

#init = tf.initialize_all_variables()  #在神经网络中初始化变量,不加括号的话,会报错:TypeError: Fetch argument <function should_use_result.<locals>.wrapped at 0x000000000C0A36A8> has invalid type <class 'function'>,
init = tf.global_variables_initializer()
#创建tensorflow结构结束

sess = tf.Session()
sess.run(init)  #run开始,sess类似指针,激活神经网络结构  很重要

#开始训练
for step in range(201):
    sess.run(train)
    if step % 20 == 0:  #每隔20步进行一次打印
        print(step,sess.run(Weights),sess.run(biases))


'''
0 [0.37426233] [0.21016647]
20 [0.16258861] [0.26858974]
40 [0.11535621] [0.29229346]
60 [0.10376766] [0.2981092]
80 [0.1009244] [0.2995361]
100 [0.10022683] [0.29988617]
120 [0.10005568] [0.2999721]
140 [0.10001367] [0.29999316]
160 [0.10000337] [0.2999983]
180 [0.10000083] [0.2999996]
200 [0.10000022] [0.29999992]

'''

7.认识Session会话

Tensorflow 中的  Session 是 为了控制,和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结果, 或者是你所要运算的部分.

 

"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'  #https://blog.csdn.net/hq86937375/article/details/79696023

matrix1 = tf.constant([[3, 3]])
matrix2 = tf.constant([[2],
                       [2]])
product = tf.matmul(matrix1, matrix2)  # matrix multiply np.dot(m1, m2)

# method 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()

# method 2
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)

'''
12
'''

8.变量

import tensorflow as tf

state = tf.Variable(0,name="counter") #定义state变量,必须是用Variable,才能说明是变量

one = tf.constant(1)  #定义常量one
new_value = tf.add(state,one)  #在state的基础上加one,将值赋值给new_value
update = tf.assign(state,new_value)  #将new_value的变量加载到state

init = tf.global_variables_initializer()  #初始化变量  must have if define variable

with tf.Session() as sess:
    sess.run(init)  #真正意义上初始化变量
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))
'''
1
2
3
'''

9.placeholder传入值

placeholder 是 Tensorflow 中的占位符,暂时储存变量.Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数据 sess.run(***, feed_dict={input: **}).

import tensorflow as tf

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1,input2)

with tf.Session() as sess:
    print(sess.run(output,feed_dict={input1:[7],input2:[2]}))  #字典形式传入

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值