-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyTorchTests.py
380 lines (290 loc) · 12.4 KB
/
PyTorchTests.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# coding: utf-8
# In[ ]:
#External Modules
import numpy as np
from torch.autograd import Variable
from torch.autograd import Function
import torch.nn as nn
import torch
import pdb
from CNN_input import CNNInputDataset
from ast import literal_eval
# In[396]:
def test_forward():
model = nn.Sequential(
ChemConv(2,14),
nn.ReLU(inplace = True),
nn.Linear(16,1, bias=True),
Average()
)
# storage_directories = ['/Users/michaeljstatt/Documents/CS230_Final_Project/data/storage_directories/150868984252']
storage_directories = ['/Users/brohr/Documents/Stanford/Research/scripts/ML/CS230_Final_Project/150868984252']
dataset = CNNInputDataset(storage_directories)
return model(dataset[0])
def test_backward():
y_pred = test_forward()
y_actual = Variable(-0.35*torch.ones((1)))
loss_fn = nn.MSELoss()
loss = loss_fn(y_pred, y_actual)
print y_pred, y_actual, loss
loss.backward()
def test_that_works():
inp = Variable(torch.randn((10)))
model = nn.Linear(10,1)
y_pred = model(inp)
y_actual = Variable(1.3*torch.ones((1)))
loss_fn = nn.MSELoss()
pdb.set_trace()
loss = loss_fn(y_pred, y_actual)
# print y_pred, y_actual, loss
# pdb.set_trace()
loss.backward()
def store_params(model):
p = []
for i in model.parameters():
p.append(i)
return p
# In[476]:
class ChemConvFunction(Function):
@staticmethod
def forward(ctx, connectivity, node_feature_matrix, filters):
"""
connectivity is a matrix describing the degree to which each pair of
atoms is connected
node_feature_matrix is size N x F+2 where N is the number of atoms in the cell
and F is the number of filters in the previous conv layer. The 2 indicates that
strength and distance have been included as features at each layer
filters is a matrix of size L x F_prev
where L is the "number of atoms" in the filter, and F is the number of filters
in the previous layer.
"""
N = len(node_feature_matrix)
F = len(filters)
node_connection_matrices, atom_index_vectors = make_convlayer_input_matrix(connectivity,node_feature_matrix)
output = torch.zeros((N, F+2))
ctx.ordered_connection_matrices = []
ctx.atom_index_vectors = []
ctx.filters_used = []
for i_node, (node_connection_matrix, atom_index_vector) in enumerate(zip(node_connection_matrices, atom_index_vectors)):
for i_filter, f in enumerate(filters):
conv_result, ordered_connection_matrix, atom_index_vector, filter_used = convolution_operation(node_connection_matrix, atom_index_vector, f)
ctx.ordered_connection_matrices.append(ordered_connection_matrix)
ctx.atom_index_vectors.append(atom_index_vector)
ctx.filters_used.append(filter_used)
output[i_node, i_filter] = conv_result
# ctx.save_for_backward(connectivity, node_feature_matrix, filters, output)
return output
@staticmethod
def backward(ctx, grad_output):
raise NotImplementedError
grad_input = None
grad_filters = None
for a,b,c in zip(ctx.ordered_connection_matrices, ctx.atom_index_vectors, ctx.filters_used):
pass # some += statement or statements
return grad_input, grad_filters
def convolution_operation(node_connection_matrix, atom_index_vector, filt):
ordered_connection_matrix, atom_index_vector = order_input(node_connection_matrix, atom_index_vector, filt)
if ordered_connection_matrix.shape[0] < filt.shape[0]:
filt = filt[:ordered_connection_matrix.shape[0]]
output = torch.sum(torch.mul(ordered_connection_matrix,filt))
return output, ordered_connection_matrix, atom_index_vector, filt
class ChemConv(nn.Module):
def __init__(self, in_depth, out_depth):
super(ChemConv, self).__init__()
self.in_depth = in_depth
self.out_depth = out_depth
######FILTER DIMENSION#########
filter_dimension = 13
self.filters = nn.Parameter(torch.Tensor(out_depth,filter_dimension,in_depth+2))
self.filters.data.normal_()
self.filters.data *= 0.01
def forward(self, input):
(connectivity, node_feature_matrix, energy) = input
return ChemConvFunction.apply(connectivity, node_feature_matrix, self.filters)
def order_input(node_connection_matrix, atom_index_vector, filt):
"""
node_connection_matrix :: e x F+1 matrix, where e is the number of edges of the
node that we are applying filter to and F is the number
of filters in the previous convlayer (or 2 for init data)
filter :: fx x fy matrix, where fx is the arity of the filter
and fy is the number of edges captured by the filter
NOTE: for speed, we could build up the convolution operation inside the
for loop (conv += np.dot(node_connection_matrix[best_fit]
"""
sum_check = sum(atom_index_vector) # for assert check at end of function
node_connection_tensor = torch.from_numpy(node_connection_matrix)
output_dimensions = (min(node_connection_matrix.shape[0],filt.shape[0]),filt.shape[1]) #smaller of (num edges this node has, length of filter)
output = torch.zeros(output_dimensions)
output[0] = node_connection_tensor[0]
filtered_numpy = np.delete(np.array(node_connection_tensor),0,0) # Brian
ordered_atom_index_vector = [] # Brian
if len(filt)>0:
i = 1
ordered_atom_index_vector.append(atom_index_vector[0]) # Brian
del atom_index_vector[0] # Brian
for filtrow in filt[1:]: # presuming no atoms have NO bonds
if i<output.shape[0]:
node_connection_tensor = torch.from_numpy(filtered_numpy) # Brian
scores = torch.matmul(node_connection_tensor,filtrow.double())
best_fit = np.argmax(scores)
ordered_atom_index_vector.append(atom_index_vector[best_fit]) # Brian
del atom_index_vector[best_fit] # Brian
output[i] = node_connection_tensor[best_fit]
filtered_numpy = np.delete(np.array(node_connection_tensor),best_fit,0)
i+=1
assert sum_check == sum(ordered_atom_index_vector) # these vectors should contain the same indices in a possibly different order, so they should have the same sum
return output, ordered_atom_index_vector
def make_convlayer_input_matrix(connectivity,node_feature_matrix):
"""
Takes a connectivity list and node_feature matrix to produce an input list
(of np arrays) for the conv layer
connectivity :: [?x4] (list of length N)
node_feature :: NxF matrix of features
output ::[?xF+2] (list of length N)
"""
output = []
atom_index_vectors = []
for i,connections in enumerate(connectivity):
this_node = np.append(node_feature_matrix[i],[0,0])
newatom = [this_node]
atom_index_vector = [i]
for to_node, strength, dist in connections:
node_feature = node_feature_matrix[int(to_node)]
atom_index_vector.append(int(to_node))
newatom.append(np.append(node_feature,[strength,dist])) # num_features + 2
output.append(np.array(newatom))
atom_index_vectors.append(atom_index_vector)
assert len(atom_index_vector) == len(newatom)
assert len(atom_index_vectors) == len(output)
return output, atom_index_vectors
# In[477]:
##### ChemConv Test
model = nn.Sequential(
ChemConv(2,14),
nn.ReLU(inplace = True),
nn.Linear(16,1, bias=True),
Average()
)
# storage_directories = ['/Users/michaeljstatt/Documents/CS230_Final_Project/data/storage_directories/150868984252']
storage_directories = ['/Users/brohr/Documents/Stanford/Research/scripts/ML/CS230_Final_Project/150868984252']
dataset = CNNInputDataset(storage_directories)
y_pred = model(dataset[0])
print y_pred
# pdb.set_trace()
y_actual = Variable(-0.35*torch.ones((1)))
loss_fn = nn.MSELoss()
loss = loss_fn(y_pred, y_actual)
# print y_pred, y_actual, loss
p=1
p = store_params(model)
loss.backward()
# In[480]:
class MyLinearFunction(Function):
# Note that both forward and backward are @staticmethods
@staticmethod
# bias is an optional argument
def forward(ctx, input, weight, bias=None):
ctx.save_for_backward(input, weight, bias)
output = input.matmul(weight.t())
if bias is not None:
output += bias.unsqueeze(0).expand_as(output)
return output
# This function has only a single output, so it gets only one gradient
@staticmethod
def backward(ctx, grad_output):
# This is a pattern that is very convenient - at the top of backward
# unpack saved_tensors and initialize all gradients w.r.t. inputs to
# None. Thanks to the fact that additional trailing Nones are
# ignored, the return statement is simple even when the function has
# optional inputs.
input, weight, bias = ctx.saved_variables
grad_input = grad_weight = grad_bias = None
# These needs_input_grad checks are optional and there only to
# improve efficiency. If you want to make your code simpler, you can
# skip them. Returning gradients for inputs that don't require it is
# not an error.
if ctx.needs_input_grad[0]:
grad_input = grad_output.matmul(weight)
if ctx.needs_input_grad[1]:
grad_weight = grad_output.t().mm(input)
if bias is not None and ctx.needs_input_grad[2]:
grad_bias = grad_output.sum(0).squeeze(0)
return grad_input, grad_weight, grad_bias
class MyLinearModule(nn.Module):
def __init__(self, input_features, output_features, bias=True):
super(MyLinearModule, self).__init__()
self.input_features = input_features
self.output_features = output_features
# nn.Parameter is a special kind of Variable, that will get
# automatically registered as Module's parameter once it's assigned
# as an attribute. Parameters and buffers need to be registered, or
# they won't appear in .parameters() (doesn't apply to buffers), and
# won't be converted when e.g. .cuda() is called. You can use
# .register_buffer() to register buffers.
# nn.Parameters can never be volatile and, different than Variables,
# they require gradients by default.
self.weight = nn.Parameter(torch.Tensor(output_features, input_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(output_features))
else:
# You should always register all possible parameters, but the
# optional ones can be None if you want.
self.register_parameter('bias', None)
# Not a very smart way to initialize weights
self.weight.data.uniform_(-0.1, 0.1)
if bias is not None:
self.bias.data.uniform_(-0.1, 0.1)
def forward(self, input):
# See the autograd section for explanation of what happens here.
return MyLinearFunction.apply(input, self.weight, self.bias)
inp = Variable(torch.randn((1,10)))
model = MyLinearModule(10,1)
y_pred = model(inp)
y_actual = Variable(1.3*torch.ones((1)))
loss_fn = nn.MSELoss()
loss = loss_fn(y_pred, y_actual)
# print y_pred, y_actual, loss
# pdb.set_trace()
loss.backward()
# f = y_pred.grad_fn
# print f
# f.apply(2)
p = store_params(model)
# In[479]:
#### Average Test
class AverageFunction(Function):
@staticmethod
def forward(ctx, input):
ctx.N = input.shape[0]
output = torch.mean(input)
output = output*torch.ones(1)
return output
@staticmethod
def backward(ctx, grad_output):
grad_input = Variable(grad_output.data/float(ctx.N)*torch.ones(ctx.N,1))
return grad_input
class Average(nn.Module):
def __init__(self):
super(Average, self).__init__()
def forward(self, input):
return AverageFunction.apply(input)
# In[478]:
inp = Variable(torch.randn((10,5))) #10 atoms (N), 5 descriptors (D)
# model = nn.Sequential(
# nn.Linear(5,1, bias=True),
# Average()
# )
model = nn.Sequential(
MyLinearModule(5,1, bias=True),
Average()
)
# model = nn.Sequential(
# MyLinearModule(5,1, bias=True)
# )
y_pred = model(inp)
# y_pred.backward()
y_actual = Variable(1.3*torch.ones((1)))
loss_fn = nn.MSELoss()
loss = loss_fn(y_pred, y_actual)
loss.backward()
# In[ ]: