<jupyter_start><jupyter_text>Emerging Technologies Project 2020*** Required imports<jupyter_code># Neural networks.
import tensorflow.keras as kr
# Numerical arrays
import numpy as np
# Data frames.
import pandas as pd
# Plotting
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>*** Set up plot<jupyter_code># Plot style.
plt.style.use("ggplot")
# Plot size.
plt.rcParams['figure.figsize'] = [14, 8]<jupyter_output><empty_output><jupyter_text>*** Read data and split up columns<jupyter_code># Read power data from csv file into np array, ignore the string elements [1]
data = np.genfromtxt('./misc/powerproduction.csv', delimiter=",",skip_header=1)
# Define train
train = pd.DataFrame()
# Remove rows with values of zero [2]
refineddata = data[(data != 0).all(axis=1)]
# Add speed and power columns to train
train['speed'] = refineddata[:,0]
train['power'] = refineddata[:,1]
# Show refined data
train<jupyter_output><empty_output><jupyter_text>*** Initialize model<jupyter_code># Create a neural network
model = kr.models.Sequential()<jupyter_output><empty_output><jupyter_text>*** Testing with different single layer configurations<jupyter_code># Testing single layer networks

# Add a layer and compile
#model.add(kr.layers.Dense(1, input_shape=(1,), activation="linear", kernel_initializer='ones', bias_initializer='zeros'))

# Better than ones & zeros: random_normal - loss 230
#model.add(kr.layers.Dense(1, input_shape=(1,), activation="linear", kernel_initializer='random_normal', bias_initializer='random_normal'))

# Less acurate: glorot_uniform - 260
#model.add(kr.layers.Dense(1, input_shape=(1,), activation="linear", kernel_initializer='glorot_uniform', bias_initializer='glorot_uniform'))<jupyter_output><empty_output><jupyter_text>*** Add 3 layers to networkHere I am adding three layers of neurons to the network. Layers 1 and 2 have 3 neurons each of activation type Sigmoid. Layer 3 has one neuron of activation type Linear. By doing this I got the loss down from approximately 260 to 16.1256. The result of 260 loss was with a single layer which had only one neuron whose activation type was linear (tested in cell above).<jupyter_code># 3 neurons, sigmoid, input 1
model.add(kr.layers.Dense(3, input_shape=(1,), activation="sigmoid", kernel_initializer='random_normal', 
                          bias_initializer='random_normal'))
# 3 neurons, sigmoid, input 3
model.add(kr.layers.Dense(3, input_shape=(3,), activation="sigmoid", kernel_initializer='random_normal', 
                          bias_initializer='random_normal'))
# 1 neurons, linear, input 3
model.add(kr.layers.Dense(1, input_shape=(3,), activation="linear", kernel_initializer='random_normal', 
                          bias_initializer='random_normal'))<jupyter_output><empty_output><jupyter_text>*** Compile model<jupyter_code># Compile Network
model.compile('adam', loss='mean_squared_error')<jupyter_output><empty_output><jupyter_text>*** Train model Loading our refined csv data into the model. And setting the epochs to 3000, I show, from the comparison below that it is the most efficient in terms of accuracy vs running time. **Epoch number / loss correlation**1000 epoch loss is 793.14342000 epoch loss is 66.31113000 epoch loss is 16.12564000 epoch loss is 15.6183*Note: These losses will vary slightly each time the note-book is run.*<jupyter_code># Train the neural network on our training data.
model.fit(train['speed'], train['power'], epochs=3000)<jupyter_output>Epoch 1/3000
15/15 [==============================] - 1s 3ms/step - loss: 4690.6767
Epoch 2/3000
15/15 [==============================] - 0s 3ms/step - loss: 4159.4781
Epoch 3/3000
15/15 [==============================] - 0s 2ms/step - loss: 4405.0764
Epoch 4/3000
15/15 [==============================] - 0s 2ms/step - loss: 4449.2504
Epoch 5/3000
15/15 [==============================] - 0s 3ms/step - loss: 4363.0062
Epoch 6/3000
15/15 [==============================] - 0s 2ms/step - loss: 4297.0624
Epoch 7/3000
15/15 [==============================] - 0s 2ms/step - loss: 4678.9287
Epoch 8/3000
15/15 [==============================] - 0s 3ms/step - loss: 4589.7273
Epoch 9/3000
15/15 [==============================] - 0s 2ms/step - loss: 4563.9498
Epoch 10/3000
15/15 [==============================] - 0s 2ms/step - loss: 4734.2495
Epoch 11/3000
15/15 [==============================] - 0s 2ms/step - loss: 4218.8690
Epoch 12/3000
15/15 [==============================] - 0s 2ms/step - loss:[...]<jupyter_text>*** Plot graphA graph makes it easier to grasp the idea, this graph is plotted as; Power over Time. **Green:** Power data from csv. **Red:** Predicted power output.<jupyter_code># Title
plt.title('Actual & Prediction')
# actual - green
plt.plot(train['speed'],train['power'], '.', color="green", label='actual data')
# join the dots
plt.plot(train['speed'],train['power'], color="orange", label='actual joined', linewidth = 0.4)
# prediction - red
plt.plot(train['speed'],model.predict(train['speed']), color="red", label='prediction')
# clean up output
plt.xlabel('Wind Speed')
plt.ylabel('Power Output')
plt.legend()
# take a random sample for debugging
#model.predict([1.0, 9.0, 29.0, 70.0])<jupyter_output><empty_output><jupyter_text>Save Model<jupyter_code># Save Model
model.save('model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19142161453455703, 'fraction_numerical': 0.06368233751638884, 'mean_word_length': 5.768060836501901, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '547131', 'license': 'MIT', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1657, 'n_words': 481, 'path': 'Emerging_Technology_Project.ipynb', 'repo_name': 'DarraghLally/Emerging_Technologies_Project', 'size': '5339'}