waiting  P54 shuffle data

03_Lecture note_Linear and Logistic Regression

学习点1:

python的地址输入是要不能用正斜杠\的,要用  /  来做地址分段。 比如:

# 打开一个文件
f = open("/tmp/foo.txt", "w") f.write( "Python 是一个非常好的语言。\n是的,的确非常好!!\n" ) # 关闭打开的文件
f.close()

Birth rate - life expectancy code:

""" Solution for simple linear regression example using tf.data
Created by Chip Huyen (chiphuyen@cs.stanford.edu)
CS20: "TensorFlow for Deep Learning Research"
cs20.stanford.edu
Lecture 03
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']=''
import time import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf import utils DATA_FILE = 'data/birth_life_2010.txt' # Step 1: read in the data
data, n_samples = utils.read_birth_life_data(DATA_FILE) # Step 2: create Dataset and iterator
dataset = tf.data.Dataset.from_tensor_slices((data[:,0], data[:,1])) iterator = dataset.make_initializable_iterator()
X, Y = iterator.get_next() # Step 3: create weight and bias, initialized to 0
w = tf.get_variable('weights', initializer=tf.constant(0.0))
b = tf.get_variable('bias', initializer=tf.constant(0.0)) # Step 4: build model to predict Y
Y_predicted = X * w + b # Step 5: use the square error as the loss function
loss = tf.square(Y - Y_predicted, name='loss')
# loss = utils.huber_loss(Y, Y_predicted) # Step 6: using gradient descent with learning rate of 0.001 to minimize loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss) start = time.time() //开始时,记录一次时间
with tf.Session() as sess:
# Step 7: initialize the necessary variables, in this case, w and b
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph) # Step 8: train the model for 100 epochs
for i in range(100):
sess.run(iterator.initializer) # initialize the iterator
total_loss = 0
try:
while True:
_, l = sess.run([optimizer, loss])
total_loss += l
except tf.errors.OutOfRangeError:
pass print('Epoch {0}: {1}'.format(i, total_loss/n_samples)) # close the writer when you're done using it
writer.close() # Step 9: output the values of w and b
w_out, b_out = sess.run([w, b])
print('w: %f, b: %f' %(w_out, b_out))
print('Took: %f seconds' %(time.time() - start)) # plot the results
plt.plot(data[:,0], data[:,1], 'bo', label='Real data')
plt.plot(data[:,0], data[:,0] * w_out + b_out, 'r', label='Predicted data with squared error')
# plt.plot(data[:,0], data[:,0] * (-5.883589) + 85.124306, 'g', label='Predicted data with Huber loss')
plt.legend()
plt.show()

CS20Chapter3的更多相关文章

随机推荐

  1. Grpc helloworld demo的经验

    GreeterGrpc.java这个文件是插件protoc-gen-grpc-java生成的 刚开始直接用类似如下的指令无法生成GreeterGrpc.java文件  protoc --java_ou ...

  2. Spring Boot 概述

    spring boot 的功能: 1.自动配置 2.起步依赖 3.Actuator hello word: http://start.spring.io 中按需生产spring boot项目,然后倒入 ...

  3. 工作经验:mybatis 处理 oracle Long 类型

    前言:mybatis 接收 oracle 中 LONG 类型的,报错:无效的列类型: getCLOB not implemented for class oracle.jdbc.driver.T4CL ...

  4. Liunx一些命令

    1.设置管理员的密码:sudo passwd root2.获取管理员权限su root3.查看IP地址sudo ifconfig -a4.创建一个文件sudo touch test.txt5.创建一个 ...

  5. CSS核心内容:层叠和继承

    实际的工作中,当有多个选择器作用在一个元素上时,哪个规则最终会应用到元素上?其实这是通过层叠机制来控制的,这也和样式继承(元素从其父元素那里获得属性值)有关.样式的优先级就是这样的一个机制,它包括!i ...

  6. Android深入理解Context(三)Context完全解析

      1· Context类型        我们知道,Android应用都是使用Java语言来编写的,那么大家可以思考一下,一个Android程序和一个Java程序,他们最大的区别在哪里?划分界限又是 ...

  7. 3 使用selenium模拟登录csdn

    之前通过F12开发者模式调试,获取网站后台服务器验证用户名和密码的URL之后,再构造post数据的方式会存在一个问题,就是对目标网站的验证机制不明确,构造post数据除了用户名和密码之外,还可能有更复 ...

  8. js实现查找字符串出现最多的字符和次数

    代码如下: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...

  9. Tcpdump一些常用指令

    1.tcpdump安装:yum install tcpdump 2.关键字介绍 类型关键字: 指定主机 host 192.168.1.111 指定网络地址 net 202.0.0.0 指定端口 por ...

  10. [翻译] LazyFadeInView 渐入显示text文本

    LazyFadeInView 渐入显示text文本 https://github.com/itouch2/LazyFadeInView LazyFadeInView is a cool way to ...