def readFile(self, params): if os.path.exists(FILE_NAME): temp = readWeights.readMatrix( FILE_NAME, [[64, 3, 5, 5], [64], [64, 64, 5, 5], [64], [384, 4096], [384], [192, 384], [192], [10, 192], [10]]) #temp = readWeights.readMatrix( FILE_NAME, [ [6, 3, 5, 5], [6], [16, 6, 5, 5], [16], [120, 400], [120], [84, 120], [84], [10, 84], [10] ] ) for p, t in zip(params, temp): p.data.copy_(torch.from_numpy(t)) return True else: False
return loss def backwardPass(self, func, create_graph): g = autograd.grad(func, self.parameters(), create_graph=create_graph) return g model = TestCNN() model.double() X = np.asarray([1 for i in range(1, 37)]) X = X.reshape(1, 1, 6, 6) X = np.tile(X, (1, 1, 1, 1)) print('Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix('../../cuda_dataset.txt', [[1, 1, 6, 6]]) print('Done reading the Dataset from the file... ') X = temp[0] print(X) print print Y = np.asarray([0]) # Weights here. cw = np.array([0.1 for i in range(1, 10)]) cw = cw.reshape(1, 1, 3, 3) cw = cw.T cw = np.tile(cw, (1, 1, 1, 1)) cb = np.array([0.1])
print('grad input', grad_input) print('grad out', grad_output) def POOLHOOK(module, grad_input, grad_output): print('POOLHOOK -- > module hook') print('grad input', grad_input) print('grad out', grad_output) #X = np.asarray( [ 1 for i in range( 1, 17 ) ] ) #X = np.random.randn( 6, 6) #X = X.reshape( 4, 4 ) #X = np.tile( X, ( 1, 1, 1, 1) ) temp = readWeights.readMatrix('../../cuda_dataset.txt', [[1, 1, 4, 4]]) X = np.reshape(temp[0], (1, 1, 4, 4), order='F') X = temp[0] #data = torch.from_numpy( np.asarray( [ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 ], dtype=float ) ) #data = torch.unsqueeze( data, 0 ) #data = Variable( data, requires_grad=True ) data = Variable(torch.from_numpy(X).type(torch.DoubleTensor), requires_grad=True) print(data) target = torch.from_numpy(np.asarray([3])).type(torch.LongTensor) #target = torch.from_numpy( np.asarray( [ 0 ] ) ) target = Variable(target)
return g def hv_r( self, gradient, vector ): hv = autograd.grad( gradient, self.parameters (), grad_outputs=vector, only_inputs=True, retain_graph=True) return [ hd.detach () for hd in hv ] model = TestCNN () model.double () X = np.asarray( [1 for i in range( 1, (2*14*14 + 1) ) ] ) X = X.reshape( 1, 2, 14, 14 ) #X = np.tile( X, (1, 1, 1, 1) ) print( 'Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix( '../../cuda_dataset.txt', [ [1, 2, 14, 14] ] ) print( 'Done reading the Dataset from the file... ') X = temp[ 0 ] #X[ 0, 0, :, : ] = X[ 0, 0, :, : ].T #X[ 0, 1, :, : ] = X[ 0, 1, :, : ].T print( X ) print print Y = np.asarray( [ 0 ] ) # Weights here. cw1 = np.array( [ 0.1 for i in range( 1, 4 * 2 * 3 * 3 + 1 ) ] )
self.parameters(), grad_outputs=vec, retain_graph=True, only_inputs=True) return hv model = TestCNN() model.double() #X = np.asarray( [1 for i in range( 1, 73 ) ] ) #X = X.reshape( 1, 2, 6, 6 ) #X = np.tile( X, (1, 1, 1, 1) ) print('Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix('../../cuda_dataset.txt', [[DATASET_SIZE, 2, 16, 16]]) print('Done reading the Dataset from the file... ') X = temp[0] print(X) print print Y = np.asarray(DATASET_SIZE * [0]) # Weights here. cw = np.array([0.1 for i in range(1, 73)]) cw = cw.reshape(4, 2, 3, 3) cw = cw.T cw = np.tile(cw, (1, 1, 1, 1))
#return torch.cat( [ grad.contiguous ().view( -1 ) for grad in g if grad is not None ] ) def hv( self, fun, grad, vec ): hv = autograd.grad( grad, self.parameters (), grad_outputs=vec, retain_graph=True, only_inputs=True ) return hv model = TestCNN () model.double () #X = np.asarray( [1 for i in range( 1, 73 ) ] ) #X = X.reshape( 1, 2, 6, 6 ) #X = np.tile( X, (1, 1, 1, 1) ) print( 'Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix( '../../cuda_dataset.txt', [ [DATASET_SIZE, 2, 6, 6] ] ) print( 'Done reading the Dataset from the file... ') X = temp[ 0 ] print( X ) print print Y = np.asarray( DATASET_SIZE * [ 0 ] ) # Weights here. cw = np.array( [ 0.1 for i in range( 1, 73 ) ] ) cw = cw.reshape( 4, 2, 3, 3 ) cw = cw.T
BATCH_SIZE = 1 model = LeNetCIFAR(num_classes=10, activation='softplus') model.double() model.cuda() criterion = nn.CrossEntropyLoss() #criterion = nn.CrossEntropyLoss (size_average=False) model.setLossFunction(criterion) X = np.asarray([1 for i in range(1, (3 * 32 * 32 + 1))]) X = X.reshape(1, 3, 32, 32) #X = np.tile( X, (1, 1, 1, 1) ) print('Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix('../../cuda_dataset.txt', [[BATCH_SIZE, 3, 32, 32]]) print('Done reading the Dataset from the file... ') #X = temp[ 0 ] X = np.reshape(temp[0], (BATCH_SIZE, 3, 32, 32), order='F') print(X) print print Y = np.asarray([0 for i in range(BATCH_SIZE)]) # Weights here. print('Reading Weights from the matrix file.... ') temp = readWeights.readMatrix( '../../cuda_weights.txt', [[6, 3, 5, 5], [6], [16, 6, 5, 5], [16], [120, 400], [120], [84, 120],
def backwardPass(self, func, create_graph): grad = autograd.grad(func, self.parameters(), create_graph=create_graph) return grad model = TestCNN() model.double() X = np.asarray([1 for i in range(1, (2 * 14 * 14 + 1))]) X = X.reshape(1, 2, 14, 14) #X = np.tile( X, (1, 1, 1, 1) ) print('Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix('../../cuda_dataset.txt', [[DATASET_SIZE, 2, 4, 4]]) print('Done reading the Dataset from the file... ') X = temp[0] #X[ 0, 0, :, : ] = X[ 0, 0, :, : ].T #X[ 0, 1, :, : ] = X[ 0, 1, :, : ].T print(X[0, 0, :, :]) print print Y = np.asarray(DATASET_SIZE * [0]) # Weights here. cw1 = np.array([0.1 for i in range(1, 4 * 2 * 3 * 3 + 1)]) cw1 = cw1.reshape(4, 2, 3, 3)
def backwardPass(self, func, create_graph): grad = autograd.grad(func, self.parameters(), create_graph=create_graph) return grad model = TestCNN() model.double() X = np.asarray([1 for i in range(1, (2 * 14 * 14 + 1))]) X = X.reshape(1, 2, 14, 14) #X = np.tile( X, (1, 1, 1, 1) ) print('Reading Dataset from the matrix file.... ') temp = readWeights.readMatrix('../../cuda_dataset.txt', [[DATASET_SIZE, 1, 14, 14]]) print('Done reading the Dataset from the file... ') X = temp[0] #X[ 0, 0, :, : ] = X[ 0, 0, :, : ].T #X[ 0, 1, :, : ] = X[ 0, 1, :, : ].T print(X[0, 0, :, :]) print print Y = np.asarray(DATASET_SIZE * [0]) # Weights here. cw1 = np.array([0.1 for i in range(1, 4 * 2 * 3 * 3 + 1)]) cw1 = cw1.reshape(4, 2, 3, 3)