这是我的代码:
#+BEGIN_SRC ipython :session
net = NeuralNet(
output_node_size = 10,
hidden_layers_node_size = [512])
#+END_SRC
import csv
import pandas as pd
import numpy as np
from keras import preprocessing as pre
list_of_data_sources = [
'Data/incrementby10.txt',
'Data/incrementby20.txt',
'Data/incrementby2.txt',
'Data/incrementby30.txt',
'Data/incrementby40.txt',
'Data/incrementby5.txt',
'Data/random10reps.txt',
'Data/random5reps.text'
]
count = 1
'''
TODO: Need to find a way to use only 70% of data so 30$ can be used as validation
TODO: figure out what least common denom each len(X_data) has with each other so that we know ho many sequences we need
'''
for i in list_of_data_sources:
with open(i, 'r') as csvfile:
data = {'pitch':[],
'drive':[],
'input':[]}
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
if len(row) is 5:
del row[0]
assert(len(row)==4)
if len(row) is not 4:
continue
if (row[2].split(' ')[-1]=='inp'):
continue
if (row[3].split(' ')[-1]=='inp'):
continue
if len(row[3].split(' ')[-1]) > 5:
continue
data['pitch'].append(row[0].split(' ')[-1])
#print("oh no: %s" % row[2].split(' ')[-1])
data['drive'].append(row[2].split(' ')[-1])
#print("oh god: %s" % (row[0].split(' ')[-1]))
data['input'].append(row[3].split(' ')[-1])
#print("oh lord: %s" % (row[3].split(' ')[-1]))
#print(len(data['pitch']))
#ummmmm = pre.sequence.TimeSeriesGenerator(data, ..., sampling_rate = 0.01, length = len(data['input']), start_index = 0, end_index = 7/10 * len(data))
#print(data)
pandas_frame = pd.DataFrame.from_dict(data)
X_data = pandas_frame[['pitch','input']].values
#print(X_data.shape)
timeBoi = [i/100 for i in range(X_data.shape[0])]
time = np.asarray(timeBoi)
time = time.reshape(time.shape[0],1)
#print(timeBoi)
X_data = X_data.reshape(1, X_data.shape[0], 1, X_data.shape[1], 1)
Y_Data = pandas_frame['drive'].values
# print(Y_Data.shape)
Y_Data = Y_Data.reshape(1,Y_Data.shape[0],1)
#print(X_data.shape)
#print(Y_Data.shape)
#count+=1
net.train(X_data,Y_Data,epochs=6)
这是网(我正在使用emacs ob-ipython);
neural.py 类NeuralNet(object):
def __init__(self,
input_node_size = None, # Number of nodes in input layer
output_node_size = None, # Number of nodes in output layer
input_shape = None,
hidden_layers_node_size = [] # Number of nodes in each hidden layer
):
<<NeuralNet_init>>
<<NeuralNet_train>>
<<NeuralNet_run>>
<<NeuralNet_label>>
**初始化
顺序模型是层的线性堆栈。我们将一个图层实例列表传递给它,以创建一个神经网络。
from keras.models import Sequential
from keras import regularizers
self.model = Sequential()
让我们从Keras导入几乎一直使用的核心层。
from keras.layers import Dense, Dropout, Activation, ConvLSTM2D, Reshape
模型应该知道应该期望的输入形状。因此,我们将第一层的输入大小分开。
# First layer requires input dimension ie input_shape
self.model.add(
ConvLSTM2D(filters = 1,
kernel_size = (2, 2),
padding='same',
input_shape=(None,1,2,1),
kernel_initializer='random_uniform',
bias_initializer='zeros',
kernel_regularizer = regularizers.l2(.01),
activity_regularizer = regularizers.l1(.01),
return_sequences = True
)
)
self.model.add(Activation('relu'))
#self.model.add(Flatten())
# Add layers to model for all hidden layers
for node_size in hidden_layers_node_size:
self.model.add(
Dense(units=node_size)
)
self.model.add(Activation('relu'))
self.model.add(Dropout(0.3))
添加正则化器并不能改善模型
# Last layer requires activation to be softmax
self.model.add(Reshape((-1,1)))
self.model.add(
Dense(units=1,
activation='softmax'
)
)
# Compile model
self.model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#model.fit(x_train, y_train, epochs=5, batch_size=32)
**火车
使模型适合训练数据集
输入: train_x-训练数据 train_y-训练标签 历元-所需的x和y数据的整体上的迭代次数
返回: 没事
def train(self, train_x, train_y, epochs):
self.model.fit(train_x, train_y, epochs)
**运行
用测试数据评估模型
输入: X-测试数据 Y-测试标签 步骤-评估完成之前整个数据集的迭代次数
返回: 指标-测试损失以及 init 中定义的指标,在这种情况下为准确性
def run(self, X, Y, steps):
metrics = []
metrics = self.model.evaluate(X, Y, batch_size = 32, steps = steps)
return metrics
**标签
预测给定数据的标签
输入: X-未标记的测试数据 步骤-评估完成之前整个数据集的迭代次数
返回: 预测-数个预测数组
def label(self, X, steps):
predictions = self.model.predict(X, batch_size = 32, steps = steps)
return predictions
这是数据的形状:
X_data->(1、2682、1、2、1)//遵循这种格式
Y_Data->(1、2682、1)
(1,2043,1,2,1) (1,2043,1)
(1,19764,1,2,1) (1,19764,1)
(1,1261,1,2,1) (1,1261,1)
(1,4848,1,2,1) (1、4848、1)
(1,34220,1,2,1) (1,34220,1)
(1,18245,1,2,1) (1,18245,1)
(1,10196,1,2,1) (1,10196,1)
由于某些原因,出现此错误:
IndexError: index 206 is out of bounds for axis 1 with size 1
Apply node that caused the error: AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}(Alloc.0, TensorConstant{1}, ARange{dtype='int64'}.0, Elemwise{Cast{int32}}.0)
Toposort index: 134
Inputs types: [TensorType(float32, matrix), TensorType(int8, scalar), TensorType(int64, vector), TensorType(int32, vector)]
Inputs shapes: [(2682, 1), (), (2682,), (2682,)]
Inputs strides: [(4, 4), (), (8,), (4,)]
Inputs values: ['not shown', array(1, dtype=int8), 'not shown', 'not shown']
Outputs clients: [[Reshape{3}(AdvancedIncSubtensor{inplace=False, set_instead_of_inc=True}.0, MakeVector{dtype='int64'}.0)]]
Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-52-54d679ced0ce>", line 3, in <module>
hidden_layers_node_size = [512])
File "<ipython-input-51-ad7af15322e6>", line 55, in __init__
metrics=['accuracy'])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 333, in compile
sample_weight, mask)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 403, in weighted
score_array = fn(y_true, y_pred)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/losses.py", line 73, in sparse_categorical_crossentropy
return K.sparse_categorical_crossentropy(y_true, y_pred)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/theano_backend.py", line 1657, in sparse_categorical_crossentropy
target = T.extra_ops.to_one_hot(target, nb_class=output.shape[-1])
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
我了解它出于某种原因试图从(...,1)获取索引206,但是idk如何解决它...请帮助! (也为草率的代码感到抱歉,不知道为什么它不认为某些不是python,而有些则不是)