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. 思维导图_Python_内置函数

  2. [POI2007]EGZ-Driving Exam

    能到达所有路的充要条件是能到达左右两端的路 用vector反向建边对每条路左右分别求个最长不上升子序列 预处理出每条路向左向右分别需要多建多少路才能到达最左端和最右端 然后跑个\(\Theta(n)\ ...

  3. laravel开发之-网站初建

    1 cmd 打开电脑命令窗口 2 目录切换到网站根目录 3 输入命令:php artisan serve 4 model 生成命令:php artisan make:model 需要生成的model名 ...

  4. 使用input做简单的上传图片

    css 代码: .container{ width: 200px; height: 200px; border: 1px solid #666; } HTML 代码: <input type=& ...

  5. <Android 基础(二十二)> EditText 无法显示完全以及尝鲜Android N

    前言 最近将Android Studio更新到了2.2 ,模拟器的Android版本也来到了最新的Nougat.很令人兴奋的一件事情呢! 对, 我就是这么没出息.文章结尾来几张图. 问题 最近遇到一个 ...

  6. 相比之前其他几个入门的, 推荐: 简单vue2 入门教程

    注意:Vue.js 不支持 IE8 及其以下 IE 版本.       具体可以看下  http://www.runoob.com/vue2/vue-tutorial.html 以下是学习过程 Vue ...

  7. #include <unistd.h> 的作用

    原文:http://blog.csdn.net/ybsun2010/article/details/24832113 由字面意思,unistd.h是unix std的意思,是POSIX标准定义的uni ...

  8. Windows 批处理(cmd/bat)常用命令教程

    Windows批处理(cmd/bat)常用命令教程 简单详细,建议收藏 常见问题: 1.如果你自己编写的.bat文件,双击打开,出现闪退 2.批处理.bat 文件中输出中文乱码 解决方法在文章末尾! ...

  9. 4.Bootstrap基础总结

    一.Bootstrap 网格系统 二.Bootstrap 排版 三.Bootstrap 代码 四.Bootstrap 表格 五.Bootstrap 表单 六.Bootstrap 按钮 七.Bootst ...

  10. “小小科技女神”与微软DigiGirlz Day的约会

    上周五在微软中国上海科技园举行的微软科技女生夏令营终于在一天“忙碌的轻松中”,伴随着师生和工程师们的欢笑结束了. 本次的微软科技女生夏令营一共有来自上海闵行区七宝中学.莘庄中学和闵行中学的共50名高中 ...