TensorFlow学习总结1

原创 2018年04月17日 17:13:39

TensorFlow学习总结

本文字代码大多来自《实战google深度学习框架》,在这里做记录。

TensorFlow简介

TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库。节点(Nodes)在图中表示数学操作,图中的线(edges)则表示在节点间相互联系的多维数据数组,即张量(tensor)。它灵活的架构让你可以在多种平台上展开计算,例如台式计算机中的一个或多个CPU(或GPU),服务器,移动设备等等。—— [ TensorFlow中文社区 ]

基本输入输出

import tensorflow as tf 
w1=tf.Variable(tf.random_normal([2,3],stddev=1 ))
w2=tf.Variable(tf.random_normal([3,1],stddev=1))

#placeholder 就是输入训练数据的入口
x=tf.placeholder(tf.float32,shape=(1,2),name='input')

a=tf.matmul(x,w1)
y=tf.matmul(a,w2)
y2=tf.matmul(a*2,w2)

with  tf.Session() as sess:
    init_op=tf.global_variables_initializer().run()
    #想查看输出,必须要在run()中,第一个参数就是获得输出,
    #同时,输入就是feed_dict的中字典形式输入,与上面的placeholder相呼应
    print(sess.run([y,y2],feed_dict={x:[[0.5,0.9]]}))

Loss中求评价:tf.reduce_mean()

在计算交叉熵或者loss时,需要把所有的y-y_的值进行计算后求平均值,这里需要tf.reduce_mean()

import tensorflow as tf 

mean=tf.reduce_mean([[2.0,4.0],[1.0,3.0]])
mean1=tf.reduce_mean([[2.0,4.0],[1.0,3.0]],0)
mean2=tf.reduce_mean([[2.0,4.0],[1.0,3.0]],1)

with tf.Session() as sess:
    print(sess.run([mean,mean1,mean2])) 

#output 
#[2.5, array([ 1.5,  3.5], dtype=float32), array([ 3.,  2.], dtype=float32)]
#可以看出和numpy.mean()相差不多

一般在神经网络的损失函数中用到,通常的句法为
这里写图片描述

关于L1,L2正则表达式

正则表达式是防止过拟合的一种方式,其中L1可以使某些参数w变为0,从而减少影响因素,L2会使某些w不会太大,不会使某一个过于夸张(自己理解):


# -*- coding: utf-8 -*-
import tensorflow as tf

weight = tf.constant([[1.0, -2.0], [-3.0, 4.0]])

with tf.Session() as sess:

    #输出为(|1|+|-2|+|-3|+|4|)*0.5=5
    print(sess.run(tf.contrib.layers.l1_regularizer(0.5)(weight)))

    #输出为(1*1+(-2*-2)+(3*3)+4*4)*0.5=7.5
    print(sess.run(tf.contrib.layers.l2_regularizer(0.5)(weight)))

关于损失函数的合集tf.add_to_collection(‘losses’,mse_loss)

tensorflow的collection是可以全局存储和获取的,可以收集多个losses:

import tensorflow as tf 

其中,y_ 和cur_layer都可以是数组
mse_loss1=tf.reduce_mean(tf.square(y_ - cur_layer))
mse_loss2=tf.reduce_mean(tf.square(y_ - cur_layer))

tf.add_to_collection('losses',mse_loss1)
tf.add_to_collection('losses',mse_loss2)

#在这里就可以把不同位置的'losses'集合起来
loss=tf.add_n(tf.get_collection('losses'))

TensorFlow 即时计算

TensorFlow平常必须要搭好框架再进行计算某变量值,现在有了

'''
Basic introduction to TensorFlow's Eager API.
Author: Aymeric Damien
Project: https://github.com/aymericdamien/TensorFlow-Examples/
What is Eager API?
" Eager execution is an imperative, define-by-run interface where operations are
executed immediately as they are called from Python. This makes it easier to
get started with TensorFlow, and can make research and development more
intuitive. A vast majority of the TensorFlow API remains the same whether eager
execution is enabled or not. As a result, the exact same code that constructs
TensorFlow graphs (e.g. using the layers API) can be executed imperatively
by using eager execution. Conversely, most models written with Eager enabled
can be converted to a graph that can be further optimized and/or extracted
for deployment in production without changing code. " - Rajat Monga
'''
from __future__ import absolute_import, division, print_function

import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe

# Set Eager API
print("Setting Eager mode...")
tfe.enable_eager_execution()

# Define constant tensors
print("Define constant tensors")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)

# Run the operation without the need for tf.Session
print("Running operations, without tf.Session")
c = a + b
print("a + b = %i" % c)
d = a * b
print("a * b = %i" % d)


# Full compatibility with Numpy
print("Mixing operations with Tensors and Numpy Arrays")

# Define constant tensors
a = tf.constant([[2., 1.],
                 [1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % a)
b = np.array([[3., 0.],
              [5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)

# Run the operation without the need for tf.Session
print("Running operations, without tf.Session")

c = a + b
print("a + b = %s" % c)

d = tf.matmul(a, b)
print("a * b = %s" % d)

print("Iterate through Tensor 'a':")
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])


#输出如下:
[Running] python "e:\00--code\aaa\TensorFlow\tempCodeRunnerFile.py"
WARNING:tensorflow:From C:\Users\liujun\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use the retry module or similar alternatives.
Setting Eager mode...
Define constant tensors
a = 2
b = 3
Running operations, without tf.Session
a + b = 5
a * b = 6
Mixing operations with Tensors and Numpy Arrays
Tensor:
 a = tf.Tensor(
[[ 2.  1.]
 [ 1.  0.]], shape=(2, 2), dtype=float32)
NumpyArray:
 b = [[ 3.  0.]
 [ 5.  1.]]
Running operations, without tf.Session
a + b = tf.Tensor(
[[ 5.  1.]
 [ 6.  1.]], shape=(2, 2), dtype=float32)
a * b = tf.Tensor(
[[ 11.   1.]
 [  3.   0.]], shape=(2, 2), dtype=float32)
Iterate through Tensor 'a':
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0.0, shape=(), dtype=float32)
2018-04-17 17:12:14.568104: I C:\tf_jenkins\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

[Done] exited with code=0 in 10.386 seconds
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/loovelj/article/details/79977685

人工智能tensorflow

第一节免费欢迎观看,人工智能 神经网络
  • 2018年04月10日 15:31

mac下安装tensorflow serving步骤及踩的坑

参考:https://weiminwang.blog/2017/09/12/introductory-guide-to-tensorflow-serving/ https://blog.csdn....
  • luoyexuge
  • luoyexuge
  • 2018-04-04 20:13:05
  • 57

Hacking Windows CE

--[ 2 - Windows CE Overview Windows CE is a very popular embedded operating system for PDAs and mobi...
  • TheGameIsFives
  • TheGameIsFives
  • 2007-11-12 13:32:00
  • 1571

HTTP and REST

A Beginner’s Guide to HTTP and REST by Ludovico Fischer9 Jan 2013 Difficulty:Int...
  • screaming
  • screaming
  • 2016-09-23 09:53:50
  • 445

iOS开发系列—Objective-C之Foundation框架

概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NSObject,那么这个对象到...
  • qq136501564
  • qq136501564
  • 2016-04-25 00:06:27
  • 269

[C] 跨平台使用Intrinsic函数范例1——使用SSE、AVX指令集 处理 单精度浮点数组求和(支持vc、gcc,兼容Windows、Linux、Mac)

本文面对对SSE等SIMD指令集有一定基础的读者,以单精度浮点数组求和为例演示了如何跨平台使用SSE、AVX指令集。因使用了stdint、zintrin、ccpuid这三个模块,可以完全避免手工编写汇...
  • canglangclient
  • canglangclient
  • 2018-02-08 12:03:55
  • 11

MyEclipse 8.5反编译插件jad安装方法

MyEclipse 8.5反编译插件jad安装方法  1、jad下载:    准备工作    1.下载jad.exe文件(压缩包)   2.下载jadeclipse插件:net.sf....
  • liu_dong_kang
  • liu_dong_kang
  • 2017-03-24 10:35:15
  • 128

Struts2+Spring3+MyBatis3整合以及Spring注解开发

最近在做一个SpringMVC+Spring+MyBatis的项目,突然想起以前自己要p
  • levelmini
  • levelmini
  • 2014-10-31 09:38:01
  • 3260

升级mysql 5.5.19至5.6.33(架构:mysql双主复制+keepalive)

一:实验环境 操作系统:CentOS release 6.5 (Final) mysql双主复制+keepalive实现了高可用: master1:10.192.203.201 master2:10....
  • YABIGNSHI
  • YABIGNSHI
  • 2016-11-25 16:37:37
  • 601

Introduction to Messages as to vc++

The computer is a machine that only follows instructions. It almost doesnt know anything. Becau...
  • lbsjs
  • lbsjs
  • 2005-08-26 02:02:00
  • 864
收藏助手
不良信息举报
您举报文章:TensorFlow学习总结1
举报原因:
原因补充:

(最多只允许输入30个字)