forked from kyuhyuckro/armsimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
97 lines (82 loc) · 3.03 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
import pandas as pd
import os
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import train_test_split
#from sklearn.model_selection import cross_val_score
#from sklearn import cross_validation
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
#from shuffle import shuffle_in_unison
from keras import backend as K
import scipy.io
#neural network to train arm data
merged_data=pd.read_csv('merged_data_new.csv')
X=merged_data.iloc[:,1:13]
y=merged_data.iloc[:,13:]
all_times = X.iloc[:,0]
#X0 = X.iloc[:,11:12] # 11, 12, positions
#X = X.iloc[:,1:13]
#X = np.concatenate((X,X0), axis=1)
seed = 7
np.random.seed(seed)
model = Sequential()
model.add(Dense(256, input_dim=12, kernel_initializer='normal', activation='relu'))
layers_num = int(input("enter number of network layers: "))
folder_name = str(layers_num) + 'layers'
if not os.path.exists(folder_name):
os.makedirs(folder_name)
if layers_num < 3:
print("not possible for network, try again")
else:
add_layers = layers_num - 3
for i in range(add_layers):
model.add(Dense(128, kernel_initializer='normal', activation='relu'))
model.add(Dense(10, kernel_initializer='normal', activation='relu'))
model.add(Dense(5, kernel_initializer='normal'))
model.compile(loss='mean_squared_error', optimizer='adam')
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model.summary()
num_layers = len(model.layers)
num_neurons = 0
for layer_index in range(num_layers):
layer = model.layers[layer_index]
for i in range(1, len(layer.output.shape)):
num_neurons_in_layer = 1
num_neurons_in_layer *= int(layer.output.shape[i])
num_neurons += num_neurons_in_layer
cond = 0
C = np.empty((0,num_neurons), int)
time = np.empty((0,1), int)
print(X.shape[0])
for i in range (0,X.shape[0]-1):
x = X.iloc[i,:]
x = np.array(x)
x.shape = (1,12)
ctime = np.array(all_times[i])
ctime.shape = (1,1)
time = np.concatenate([time, ctime])
b = np.array([])
for j in range(len(model.layers)):
get_layer_output_0 = K.function([model.layers[0].input], [model.layers[j].output])
layer_output_0 = get_layer_output_0([x])[0]
a = layer_output_0.flatten()
b = np.concatenate([b, a])
C = np.vstack((C, b))
print (C.shape)
target1 = X.iloc[i,10:12]
print(X.iloc[i,10:12])
target2 = X.iloc[i+1,10:12]
if np.linalg.norm(target1 - target2) > 1e-6:
print (i)
print (target1)
print (target2)
scipy.io.savemat(folder_name + '/data1100' + str(cond) + '.mat', mdict={'C': C, 'time':time, 'target':target1})
C = np.empty((0,num_neurons), int)
time = np.empty((0,1), int)
cond = cond + 1
# cosine tuning , jpca, extending model to muscles