#!/usr/bin/python
# -*- coding: UTF- -*-
# @date: // :
# @name: first_tf_1223
# @author:vickey-wu from __future__ import print_function
import tensorflow as tf
import os # disable error
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '' # constant
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # node2 dtype also equal tf.float32 implicitly
print(node1, node2) # SSSession
sess = tf.Session() # SSSession # placeholder
a = tf.placeholder(tf.float32) # A placeholder is a promise to provide a value later
b = tf.placeholder(tf.float32)
adder_node = a + b
print(sess.run(adder_node, {a: , b: 4.5})) # fetches=a, feed_dict=dict
print(sess.run(adder_node, {a: [, ], b: [, ]})) # feed_dict=tuple # VVVariable
W = tf.Variable([.], dtype=tf.float32)
b = tf.Variable([-.], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer() # tf.Variable must be explicitly initialize, tf.constant
sess.run(init)
print(sess.run(linear_model, {x: [, , , ]})) # while x=, x=, ... linear_model = ? # loss function to evaluate a model we build is good or not
y = tf.placeholder(tf.float32) # desired values
squared_deltas = tf.square(linear_model - y) # creates a vector of error delta
loss = tf.reduce_sum(squared_deltas) # create a single scalar that abstracts the error of all examples
print(sess.run(loss, {x: [, , , ], y: [, -, -, -]})) # manually reassign the values of W and b to get optimal solution of linear_model
fixW = tf.assign(W, [-.]) # tf.assign change initialized Variable value
fixb = tf.assign(b, [.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [, , , ], y: [, -, -, -]})) # tf.train API
# machine learning is to find the correct model parameters automatically
# TensorFlow provides optimizers that slowly change each variable in order to minimize the loss function
# The simplest optimizer is gradient descent
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init)
for i in range():
sess.run(train, {x: [, , , , ], y: [, -, -, -]})
print(sess.run([W, b])) ###########################
# complete trainable linear regression model
# model parameters
W = tf.Variable([.], dtype=tf.float32)
b = tf.Variable([-.], dtype=tf.float32)
# model input and output
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
linear_model = W * x + b # loss
loss = tf.reduce_sum(tf.square(linear_model - y))
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss) # training data
x_train = [, , , ]
y_train = [, -, -, -]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range():
sess.run(train, {x: x_train, y: y_train}) # evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
######################### ##########################
import numpy as np
# import tensorflow as tf # Declare list of features
feature_columns = [tf.feature_column.numeric_column("x", shape=[])]
# an estimator is the front end to invoke training and evaluation.
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
# tensorflow provides many helper method to read and set up data sets
x_train = np.array([., ., ., .])
y_train = np.array([., -., -., -.])
x_eval = np.array([., ., ., .])
y_eval = np.array([-1.01, -4.1, -, .])
input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=None, shuffle=True
)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=, shuffle=False
)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=, shuffle=False
) # we can invoke training steps by invoking the method and passing the training data set.
estimator.train(input_fn=input_fn, steps=) # Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
print("train metrics: %r" % train_metrics)
print("eval metrics: %r" % eval_metrics) #######################

tensorflow note的更多相关文章

  1. TensorFlow Android Camera Demo 使用android studio编译安装和解决Execution failed for task ':buildNativeBazel'报错

    可以参考官网:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android#android-stud ...

  2. How to install tensorflow on ubuntu 18.04 64bit

    Ans:pip install tensorflow (note:  version number of pip and python must be  consistent)

  3. TensorFlow编译androiddemo

    首先是把tensorflow克隆到本地一份. git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git 既 ...

  4. TensorFlow Ops

    TensorFlow Ops 1. Fun with TensorBoard In TensorFlow, you collectively call constants, variables, op ...

  5. awesome-nlp

    awesome-nlp  A curated list of resources dedicated to Natural Language Processing Maintainers - Keon ...

  6. Tensorflow二分类处理dense或者sparse(文本分类)的输入数据

    这里做了一些小的修改,感谢谷歌rd的帮助,使得能够统一处理dense的数据,或者类似文本分类这样sparse的输入数据.后续会做进一步学习优化,比如如何多线程处理. 具体如何处理sparse 主要是使 ...

  7. Tensorflow mlp二分类

    只是简单demo, 可以看出tensorflow非常简洁,适合快速实验     import tensorflow as tf import numpy as np import melt_datas ...

  8. (转)The Road to TensorFlow

    Stephen Smith's Blog All things Sage 300… The Road to TensorFlow – Part 7: Finally Some Code leave a ...

  9. Tensorflow的CNN教程解析

    之前的博客我们已经对RNN模型有了个粗略的了解.作为一个时序性模型,RNN的强大不需要我在这里重复了.今天,让我们来看看除了RNN外另一个特殊的,同时也是广为人知的强大的神经网络模型,即CNN模型.今 ...

随机推荐

  1. 让ubuntu16.04开机进入命令行模式

    使用Ubuntu时,有时候我们不想开机进入桌面,想直接进入命令行,这样启动的比较快, 1.首先我们修改grub文件,改为如图所示: sudo gedit  /etc/default/grub 改完之后 ...

  2. UT源码 045

    (3)设计佣金问题的程序 commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone).手机壳(Mobile phone shell).手机贴膜(Cellp ...

  3. git 基本命令详细解释

    创建: 2017-04-05 17:04:03         2017-04-24 更新: 2017-05-16 更新: 2017-06-27  完善git remote add  更新: 2017 ...

  4. poj 3207 Ikki's Story IV - Panda's Trick【2-SAT+tarjan】

    注意到相交的点对一定要一里一外,这样就变成了2-SAT模型 然后我建边的时候石乐志,实际上不需要考虑这个点对的边是正着连还是反着连,因为不管怎么连,能相交的总会相交,所以直接判相交即可 然后tarja ...

  5. redis 发布订阅实现异步实时发短信

    redis 中发布和订阅可以实现消息的实时传输,这里我只是用它的事件驱动,当客户端发送了消息,服务器端立马可以接收指令处理相应的业务逻辑. 客户端 client.php <?php //发布 $ ...

  6. 如何修改hosts文件并生效

    hosts文件位置C:\Windows\System32\drivers\etc(可以建立一个.bat 的文件把(start "" C:\Windows\System32\driv ...

  7. Mac下怎么运行python3的py文件

    我的Mac现在是10.14.6系统,默认自带的python版本是2.7.(怎么查看版本?打开终端,输入python即可看到版本号) 由于现在需要运行python3写的py文件,需要将自带的python ...

  8. Django使用dwebsocket来通信,服务器报错[Error 10038]

    记录这次Django踩得最大的一次坑,没有之一.前前后后困扰了一周. 在使用Django的dwebsocket模块建立websocket时,不管是前端主动关闭,还是页面刷新,还是页面关闭.服务端均会报 ...

  9. 害死人不偿命的(3n+1)猜想 (15)

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n; while ( ...

  10. Jmeter之一个请求获取上一个请求的参数

    刚开始有这个需求,网上都是一些使用正则表达式的例子,苦于自己看不好正式的表达式,且响应结果稍微变一下,自己就不会写了,于是谷歌上各种搜,也阅读官网上文档,后来发现一个好的插件 Json path Ex ...