Udacity-2-4-16
来源:互联网 发布:淘宝客做推广有用吗 编辑:程序博客网 时间:2024/06/02 00:28
注意:
output_error_term = error * output * (1 - output)
hidden_error = np.dot(output_error_term,weights_hidden_output.T)
hidden_error_term = hidden_error * hidden_output * (1 - hidden_output)
output_error_term = error * output * (1 - output)
hidden_error = np.dot(output_error_term,weights_hidden_output.T)
hidden_error_term = hidden_error * hidden_output * (1 - hidden_output)
del_w_hidden_output += output_error_term*hidden_output[:,None]
del_w_input_hidden += hidden_error_term * x[:,None]
#------------程序开始了----------------------------------
import numpy as np
from data_prep import features, targets, features_test, targets_test
np.random.seed(21)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Hyperparameters
n_hidden = 2 # number of hidden units
n_out=1
epochs = 900
learnrate = 0.005
n_records, n_features = features.shape
last_loss = None
# Initialize weights
weights_input_hidden = np.random.normal(scale=1 / n_features ** .5,
size=(n_features, n_hidden))
weights_hidden_output = np.random.normal(scale=1 / n_features ** .5,
size=(n_hidden,n_out))
for e in range(epochs):
del_w_input_hidden = np.zeros(weights_input_hidden.shape)
del_w_hidden_output = np.zeros(weights_hidden_output.shape)
for x, y in zip(features.values, targets):
## Forward pass ##
# TODO: Calculate the output
hidden_input = np.dot(x, weights_input_hidden)
hidden_output = sigmoid(hidden_input)
output = sigmoid(np.dot(hidden_output,
weights_hidden_output))
## Backward pass ##
# TODO: Calculate the network's prediction error
error = y - output
# TODO: Calculate error term for the output unit
output_error_term = error * output * (1 - output)
## propagate errors to hidden layer
# TODO: Calculate the hidden layer's contribution to the error
hidden_error = np.dot(output_error_term,weights_hidden_output.T)
# TODO: Calculate the error term for the hidden layer
hidden_error_term = hidden_error * hidden_output * (1 - hidden_output)
# TODO: Update the change in weights
del_w_hidden_output += output_error_term*hidden_output[:,None]
del_w_input_hidden += hidden_error_term * x[:,None]
# TODO: Update weights
weights_input_hidden += learnrate * del_w_input_hidden / n_records
weights_hidden_output += learnrate * del_w_hidden_output / n_records
# Printing out the mean square error on the training set
if e % (epochs / 10) == 0:
hidden_output = sigmoid(np.dot(x, weights_input_hidden))
out = sigmoid(np.dot(hidden_output,
weights_hidden_output))
loss = np.mean((out - targets) ** 2)
if last_loss and last_loss < loss:
print("Train loss: ", loss, " WARNING - Loss Increasing")
else:
print("Train loss: ", loss)
last_loss = loss
# Calculate accuracy on test data
hidden = sigmoid(np.dot(features_test, weights_input_hidden))
out = sigmoid(np.dot(hidden, weights_hidden_output))
predictions = out > 0.5
accuracy = np.mean(predictions == targets_test.reshape(40,1))
print("Prediction accuracy: {:.3f}".format(accuracy))
阅读全文
0 0
- Udacity-2-4-16
- Lesson 2: Udacity Intro to Parallel Programming
- udacity 之 android学习笔记:lesson 2
- udacity android 学习笔记: lesson 4 part a
- udacity android 学习笔记: lesson 4 part b
- udacity android 实践笔记: lesson 4 part a
- udacity android 实践笔记: lesson 4 part b
- android学习笔记-udacity/Sunshine-Version-2出错
- Udacity 深度学习项目2(Project2) Image Classification 解析
- udacity assignment1
- Udacity笔记
- tensorflow的examples下Udacity中Assignment 2的problem解决方案
- Udacity深度学习(google)笔记(2)——深度神经网络, tensorflow
- Udacity机器学习(进阶)——朴素贝叶斯迷你项目2
- Udacity机器学习(进阶)——朴素贝叶斯迷你项目2
- 强烈推荐CS 课程 Udacity
- Udacity Code Review Suggestions
- Udacity:python基础
- leetcode 63. Unique Paths II
- FM调制、解调 Matlab代码
- Android Studio项目目录结构介绍
- 大数据时代开源产品。
- B-1027
- Udacity-2-4-16
- 使用一个ContentProvider操作多张表
- 单链表反转的两种实现(Java)
- Android--掌握日志工具的使用
- 名企笔试:网易游戏2016招聘笔试题(双核处理)
- python文件操作
- Kotlin基本语法
- Java中的编码问题:接收、转码、输出
- 动态网页开发知识点