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模块之numpy与pandas

    一.numpy numpy是python数据分析和机器学习的基础模块之一.它有两个作用:1.区别于list列表,提供了数组操作.数组运算.以及统计分布和简单的数学模型:2.计算速度快[甚至要由于pyt ...

  2. 【C++并发实战】(三) std::future和std::promise

    std::future和std::promise std::future std::future期待一个返回,从一个异步调用的角度来说,future更像是执行函数的返回值,C++标准库使用std::f ...

  3. C# 按部门拆分excel文件

    按照所属部门不同将excel文件拆分成多个文件 string excel_path = @"G:\zhyue\backup\2018-08-01 读取腾讯邮箱接口-获取一个月内未接收到外部邮 ...

  4. 解决API中无法使用session问题

    处理API无法使用session的方法,贴图: 1调用如下图 2.需要在Global.asax文件中配置一些东西 protected void Application_PostAuthorizeReq ...

  5. [转]乔布斯的薄伽梵歌 Steve’s Bhagavat Gita

    SRC: http://www.brucejia.net/ 2014年4月4日未分类apple.ios.steve jobs 编辑 Your time is limited, so don't was ...

  6. Java和C# RSA加解密相互通信和使用公钥加密传输

    关于JAVA和C#加解密通讯的话,可以用这个BouncyCastle插件,会帮助你解决很多问题 http://www.bouncycastle.org/ //c#使用java给的公钥进行rsa加密 p ...

  7. 笔记:Xen虚拟机如何迁移到KVM上?

    众所周知如果是在Linux上使用虚拟化技术的话,就会有基于Xen Hypervisor部署一个系统的机会.因为基于内核的虚拟机(KVM:Kernel-Based Virtual Machine)已经逐 ...

  8. Sharepoint配置Projectserver

    1   需要创建一个project server application 程序. 2  创建一个内容数据库,这个比较简单,微软文档中如下表述: 3  创建一个Project Web App  需要用命 ...

  9. [翻译] JTSlideShadowAnimation

    JTSlideShadowAnimation 效果图: JTSlideShadowAnimation allow you to reproduce the famous "slide to ...

  10. django1.8 增加注册用户其他字段(用户扩展)

    在V1.6及之后版本已经删除get_profile()方法,需要使用userprofile. 1.新建moduel,名为UserProfile: class UserProfile(models.Mo ...