Created
August 1, 2017 16:29
-
-
Save sunfuze/fb82c3bdcaf8d53a6974f44d33397d5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import matplotlib.pyplot as plt" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"拿数据" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"data_path = 'data.csv'\n", | |
"\n", | |
"data = pd.read_csv(data_path)\n", | |
"data.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"dummy_fields = ['model', 'level']\n", | |
"for each in dummy_fields:\n", | |
" dummies = pd.get_dummies(data[each], prefix=each, drop_first=False)\n", | |
" data = pd.concat([data, dummies], axis=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"quant_features = ['temp', 'otemp', 'itemp', 'ctemp', 'volume']\n", | |
"# Store scalings in a dictionary so we can convert back later\n", | |
"scaled_features = {}\n", | |
"for each in quant_features:\n", | |
" mean, std = data[each].mean(), data[each].std()\n", | |
" scaled_features[each] = [mean, std]\n", | |
" data.loc[:, each] = (data[each] - mean)/std" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"test_data = data[-200:]\n", | |
"data = data[:-200]\n", | |
"\n", | |
"target_fields = ['temp', 'model_cool', 'model_heating', 'volume_up', 'volume_down']\n", | |
"\n", | |
"features, targets = data.drop(target_fields, axis=1), data[target_fields]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"train_features, train_targets = features[:-100], targets[:-100]\n", | |
"val_features, val_targets = features[-100:], targets[-100:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class NeuralNetwork(object):\n", | |
" def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):\n", | |
" # Set number of nodes in input, hidden and output layers.\n", | |
" self.input_nodes = input_nodes\n", | |
" self.hidden_nodes = hidden_nodes\n", | |
" self.output_nodes = output_nodes\n", | |
"\n", | |
" # Initialize weights\n", | |
" self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5, \n", | |
" (self.hidden_nodes, self.input_nodes))\n", | |
"\n", | |
" self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5, \n", | |
" (self.output_nodes, self.hidden_nodes))\n", | |
" self.lr = learning_rate\n", | |
" \n", | |
" \n", | |
" self.activation_function = self.sigmoid\n", | |
" \n", | |
" def sigmoid (self, x):\n", | |
" return 1 / (1 + np.exp(-x))\n", | |
" \n", | |
" \n", | |
" def sigmoid_derivation (self, x):\n", | |
" return x * (1 - x)\n", | |
" \n", | |
" \n", | |
" def train(self, inputs_list, targets_list):\n", | |
" # convert inputs list to 2d array\n", | |
" inputs = np.array(inputs_list, ndmin=2).T\n", | |
" targets = np.array(targets_list, ndmin=2).T\n", | |
" \n", | |
"\n", | |
" hidden_inputs = np.dot(self.weights_input_to_hidden, inputs) \n", | |
" hidden_outputs = self.activation_function(hidden_inputs)\n", | |
" \n", | |
" final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs)\n", | |
" final_outputs = final_inputs\n", | |
" \n", | |
" ### Backward pass ###\n", | |
" # output errors\n", | |
" output_errors = targets - final_outputs\n", | |
" \n", | |
"\n", | |
" hidden_errors = np.dot(self.weights_hidden_to_output.T, output_errors)\n", | |
" hidden_grad = self.sigmoid_derivation(hidden_outputs)\n", | |
" \n", | |
" # update weights\n", | |
" self.weights_hidden_to_output += (self.lr * np.dot(output_errors, hidden_outputs.T))\n", | |
" self.weights_input_to_hidden += (self.lr * np.dot((hidden_errors * hidden_grad), inputs.T))\n", | |
" \n", | |
" \n", | |
" def run(self, inputs_list):\n", | |
" # Run a forward pass through the network\n", | |
" inputs = np.array(inputs_list, ndmin=2).T\n", | |
" \n", | |
" hidden_inputs = np.dot(self.weights_input_to_hidden, inputs)\n", | |
" hidden_outputs = self.activation_function(hidden_inputs)\n", | |
" \n", | |
" # Output layer\n", | |
" final_inputs = np.dot(self.weights_hidden_to_output, hidden_outputs)\n", | |
" final_outputs = final_inputs\n", | |
" \n", | |
" return final_outputs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def MSE(y, Y):\n", | |
" return np.mean((y-Y)**2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"\n", | |
"epochs = 100\n", | |
"learning_rate = 0.1\n", | |
"hidden_nodes = 2\n", | |
"output_nodes = 1\n", | |
"\n", | |
"N_i = train_features.shape[1]\n", | |
"network = NeuralNetwork(N_i, hidden_nodes, output_nodes, learning_rate)\n", | |
"\n", | |
"losses = {'train':[], 'validation':[]}\n", | |
"for e in range(epochs):\n", | |
" batch = np.random.choice(train_features.index, size=128)\n", | |
" for record, target in zip(train_features.ix[batch].values, \n", | |
" train_targets.ix[batch]['temp']):\n", | |
" network.train(record, target)\n", | |
" \n", | |
" # Printing out the training progress\n", | |
" train_loss = MSE(network.run(train_features), train_targets['temp'].values)\n", | |
" val_loss = MSE(network.run(val_features), val_targets['temp'].values)\n", | |
" sys.stdout.write(\"\\r进度: \" + str(100 * e/float(epochs))[:4] \\\n", | |
" + \"% ... Training loss: \" + str(train_loss)[:5] \\\n", | |
" + \" ... Validation loss: \" + str(val_loss)[:5])\n", | |
" \n", | |
" losses['train'].append(train_loss)\n", | |
" losses['validation'].append(val_loss)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"plt.plot(losses['train'], label='Training loss')\n", | |
"plt.plot(losses['validation'], label='Validation loss')\n", | |
"plt.legend()\n", | |
"plt.ylim(ymax=0.5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment