框架tensorflow2

TensorFlow 2

 TensorFlow 激励函数

 

TensorFlow 添加层:

思考:matmul和multiply两种乘法的区别:http://www.soaringroad.com/?p=560

[root@shenzhen tensorflow]# cat tensor5.py
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf

def add_layer(inputs,in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs
[root@shenzhen tensorflow]#

 

 

 

TensorFlow建造神经网络:

np.linspace()       在指定的间隔内返回均匀间隔的数字           (https://blog.csdn.net/you_are_my_dream/article/details/53493752    )

np.square()  计算各元素的平方   (  https://blog.csdn.net/u014595019/article/details/49686789  )

numpy添加新的维度:newaxis  :  https://blog.csdn.net/xtingjie/article/details/72510834

Tensorflow之神经网络nn模块详解  :  https://blog.csdn.net/qq_36653505/article/details/81105894

tf.nn.relu(features, name = None) 函数的作用是计算激活函数relu,即max(features, 0)。即将矩阵中每行的非最大值置0。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

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)
以上程序输出的结果是:[0. 2.]
View Code

 

tf.placeholder(dtype, shape=None, name=None)

  此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的值;

  dtype:数据类型。常用的是tf.float32,tf.float64等数值类型

  shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定;

  name:名称。

 

神经网络练习:

[root@shenzhen tensorflow]# python3 tensor5.py
2018-08-22 22:11:59.038325: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
0.21489698
0.0061830464
0.004771502
0.004203358
0.0038625475
0.0036302141
0.0034819138
0.003381687
0.0032702887
0.0031953293
0.0031516913
0.0031179215
0.0030919597
0.003072145
0.003055951
0.0030398055
0.0030263881
0.0030082215
0.0029918654
0.0029769428
[root@shenzhen tensorflow]# cat tensor5.py
#!/usr/local/bin/python3
#coding:utf-8

import tensorflow as tf
import numpy as np

def add_layer(inputs,in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

x_data = np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

xs = tf.placeholder(tf.float32,[None,1])
ys = tf.placeholder(tf.float32,[None,1])

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None)

loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),\
                       reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data, ys:y_data})
    if i % 50 == 0:
        print(sess.run(loss,feed_dict={xs:x_data, ys:y_data}))

 

 

tensorflow  优化器:

1,初级用 tf.train.GradientDescentOptimizer

2, 高级:    tf.train.MomentumOptimizer  和  tf.train.RMSPropOptimizer

解决centos中"ImportError: No module named _tkinter"问题:   https://blog.csdn.net/liuxingen/article/details/53907877

 

             

 

posted on 2018-08-22 19:44  微子天明  阅读(292)  评论(0编辑  收藏  举报

导航