def CNNForward(model, x): """Runs the forward pass. Args: model: Dictionary of all the weights. x: Input to the network. Returns: var: Dictionary of all intermediate variables. """ x = x.reshape([-1, 48, 48, 1]) h1c = Conv2D(x, model['W1']) + model['b1'] h1r = ReLU(h1c) h1p = MaxPool(h1r, 3) h2c = Conv2D(h1p, model['W2']) + model['b2'] h2r = ReLU(h2c) h2p = MaxPool(h2r, 2) h2p_ = np.reshape(h2p, [x.shape[0], -1]) y = Affine(h2p_, model['W3'], model['b3']) var = { 'x': x, 'h1c': h1c, 'h1r': h1r, 'h1p': h1p, 'h2c': h2c, 'h2r': h2r, 'h2p': h2p, 'h2p_': h2p_, 'y': y } return var
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ #MY CODE HERE I, J, _, _ = w.shape grad_y_hwnk = np.transpose(grad_y, [1, 2, 0, 3]) x_chwn = np.transpose(x, [3, 1, 2, 0]) w_ijck = np.transpose(w, [0, 1, 3, 2]) w_T = np.rot90(w_ijck, 2) #w_c = np.transpose(w_T, [0,1,3,2]) grad_w = Conv2D(x_chwn, grad_y_hwnk, pad=(I - 1, J - 1)) grad_x = Conv2D(grad_y, w_T, pad=(I - 1, J - 1)) return grad_x, np.transpose(grad_w, [1, 2, 0, 3])
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### I, J = w.shape[0:2] grad_w_t = Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), pad=(I - 1, J - 1)) grad_w = np.transpose(grad_w_t, [1, 2, 0, 3]) grad_x = Conv2D(grad_y, np.transpose(w, [0, 1, 3, 2])[::-1, ::-1, :, :], pad=(I - 1, J - 1)) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### I = w.shape[0] #height J = w.shape[1] #width P = I - 1 Q = J - 1 w = np.fliplr(w) w = np.flipud(w) w = np.transpose(w, [0, 1, 3, 2]) grad_x = Conv2D(grad_y, w, pad=(P, Q)) x = np.transpose(x, [3, 1, 2, 0]) grad_y = np.transpose(grad_y, [1, 2, 0, 3]) grad_w = Conv2D(x, grad_y, pad=(P, Q)) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) return grad_x, grad_w raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. N, H, W, C = x.shape I, J, C, K = w.shape P = (I - 1) Q = (J - 1) wFlip1 = np.flipud(w) wFlip2 = np.fliplr(wFlip1) fTran = np.transpose(wFlip2, [0, 1, 3, 2]) grad_x = Conv2D(grad_y, fTran, pad=(P, Q)) grad_w = Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), pad=(P, Q)) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. I = w.shape[0] J = w.shape[1] w_T = np.transpose(np.array(w), [0, 1, 3, 2]) for i in range(I): for j in range(J): w_T[i, j, :, :] = np.transpose(w, [0, 1, 3, 2])[I - i - 1, J - j - 1, :, :] grad_x = Conv2D(grad_y, w_T, pad=(I - 1, J - 1)) grad_w = np.transpose( Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), pad=(I - 1, J - 1)), [1, 2, 0, 3]) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. I = w.shape[0] J = w.shape[1] w_transpose = np.transpose(w, [0, 1, 3, 2]) #[I, J, K, C] w_transpose = np.flipud(w_transpose) #[I, J-j+1, K, C] w_transpose = np.fliplr(w_transpose) #[I-i+1, J-j+1, K, C] grad_x = Conv2D(grad_y, w_transpose, [I - 1, J - 1]) # grad_x = Conv2D(grad_y, w_transpose, [4, 4]) grad_y_transpose = np.transpose(grad_y, [1, 2, 0, 3]) x_transpose = np.transpose(x, [3, 1, 2, 0]) grad_w = Conv2D(x_transpose, grad_y_transpose, [4, 4]) # grad_w = Conv2D(grad_x_transpose, grad_y_transpose, [I-1, J-1]) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) return grad_x, grad_w
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. # Check all shapes coming in, and outgoing shapes to conv2D I = np.shape(w)[0] J = np.shape(w)[1] #Grad x w_T = np.transpose(np.copy(w), [0, 1, 3, 2]) w_T = w_T[::-1, ::-1, :, :] grad_x = Conv2D(grad_y, w_T, [I - 1, J - 1]) #Grad w x_T = np.transpose(np.copy(x), [3, 1, 2, 0]) grad_y_T_hwnk = np.transpose(np.copy(grad_y), [1, 2, 0, 3]) grad_w = Conv2D(x_T, grad_y_T_hwnk, [I - 1, J - 1]) #8,16,16,16 c,i,j,k grad_w = np.transpose(grad_w, [1, 2, 0, 3]) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. I = len(w) J = len(w[0]) grad_w = Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), [I - 1, J - 1]) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) wT = np.flipud(np.fliplr(w)) grad_x = Conv2D(grad_y, np.transpose(wT, [0, 1, 3, 2]), [I - 1, J - 1]) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ #=========================================================================== # xTr=np.transpose(x, [3,1,2,0]); # grad_y_tr=np.transpose(grad_y, [1,2,0,3]); # grad_w=Conv2D(xTr,grad_y_tr); # temp=np.transpose(w,[0,1,3,2]); # temp2=temp[::-1,::-1] # grad_x = Conv2D(grad_y,temp2); #=========================================================================== #=========================================================================== # I = w.shape[0] # J = w.shape[0] # grad_w = Conv2D(np.transpose(x, [3,1,2,0]),np.transpose(grad_y,[1,2,0,3]),pad=(I,J)) # w1 = np.transpose(w, [0,1,3,2]) # w2 = w1[::-1,::-1] # grad_x = Conv2D(grad_y,w2) # return grad_x, grad_w #=========================================================================== I = w.shape[0] J = w.shape[1] grad_w = Conv2D(np.transpose(x, [3,1,2,0]),np.transpose(grad_y,[1,2,0,3]),pad=(I-1,J-1)) w1 = np.transpose(w, [0,1,3,2]) w2 = w1[::-1,::-1] grad_x = Conv2D(grad_y,w2,pad=(I-1,J-1)) grad_w = np.transpose(grad_w,[1,2,0,3]) return grad_x, grad_w ########################### # Insert your code here. # grad_x = ... # grad_w = ... return grad_x, grad_w ########################### raise Exception('Not implemented')
def SubpixelConv2D(*args, **kwargs): kwargs['output_dim'] = 4 * kwargs['output_dim'] output = Conv2D(*args, **kwargs) output = tf.transpose(output, [0, 2, 3, 1]) output = tf.depth_to_space(output, 2) output = tf.transpose(output, [0, 3, 1, 2]) return output
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. # grad_x = ... # grad_w = ... # return grad_x, grad_w ########################### [I, J, C, K] = w.shape #print("x=",x.shape) #print("w=",w.shape) #print("grad_y=",grad_y.shape) x1 = np.transpose(x, [3, 1, 2, 0]) grad_y1 = np.transpose(grad_y, [1, 2, 0, 3]) wt = np.transpose(w, [0, 1, 3, 2]) wt = wt[::-1, ::-1] ## for c in xrange(C): ## for k in xrange(K): ## for i in xrange(I): ## for j in xrange(J): ## wt[i][j][k][c]=w1[I-i-1][J-j-1][c][k] """ for i in xrange(5): for j in xrange(5): print("wt",wt[i][j][0][0]) print("w1",w1[i][j][0][0]) """ #bp() grad_x = Conv2D(grad_y, wt, pad=((I - 1), (J - 1))) grad_w = Conv2D(x1, grad_y1, pad=((I - 1), (J - 1))) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) print("x", x.shape) print("grad_y", grad_y.shape) print("grad_x", grad_x.shape) print("grad_w", grad_w.shape) return grad_x, grad_w
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ # Compute the padding pad = (w.shape[0] - 1, w.shape[1] - 1) # Update grad_w x_t = np.transpose(x, [3, 1, 2, 0]) grad_y_t = np.transpose(grad_y, [1, 2, 0, 3]) grad_w = Conv2D(x_t, grad_y_t, pad) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) # Update grad_x w_t = w[::-1, ::-1, :, :] w_t = np.transpose(w_t, [0, 1, 3, 2]) grad_x = Conv2D(grad_y, w_t, pad) ''' # Another way of updating grad_x I, J, C, K = w.shape w_t = np.zeros((I, J, K, C)) for i in range(I): for j in range(J): for k in range(K): for c in range(C): w_t[i, j, k, c] = w[I - i - 1, J - j - 1, c, k] grad_x = Conv2D(grad_y, w_t, pad) ''' return grad_x, grad_w
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ #print(grad_y.shape) #print(x.shape) #print(y.shape) #print(w.shape) #filter = Conv2D(x, w); #print(con.shape) #filter_t = np.transpose(filter,[3,1,2,0]) I = w.shape[0] J = w.shape[0] grad_w = Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), pad=(I - 1, J - 1)) weight = np.transpose(w, [0, 1, 3, 2]) weight2 = weight[::-1, ::-1, :, :] grad_x = Conv2D(grad_y, weight2, pad=(I - 1, J - 1)) grad_w = np.transpose(grad_w, [1, 2, 0, 3]) ########################### # Insert your code here. #print(filter.shape) #print (filter_t.shape) #grad_x = grad_y.dot(filter_t); #print(grad_x.shape) #grad_w = return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. f_transpose = (np.transpose(w, [0, 1, 3, 2]))[::-1, ::-1] grad_x = Conv2D(grad_y, f_transpose, pad=(w.shape[0] - 1, w.shape[1] - 1)) grad_w = np.transpose( Conv2D(np.transpose(x, [-1, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), pad=(w.shape[0] - 1, w.shape[1] - 1)), [1, 2, 0, 3]) return grad_x, grad_w ########################### raise Exception('Not implemented')
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ I, J, C, K = w.shape wT = np.transpose((w[::-1, :, :, :])[:, ::-1, :, :], [0, 1, 3, 2]) grad_w = np.transpose( Conv2D(np.transpose(x, [3, 1, 2, 0]), np.transpose(grad_y, [1, 2, 0, 3]), (I - 1, J - 1)), [1, 2, 0, 3]) grad_x = Conv2D(grad_y, wT, (I - 1, J - 1)) return grad_x, grad_w
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ ########################### # Insert your code here. I,J,C,K = w.shape x1 = np.transpose(x,[3,1,2,0]) w1 = np.transpose(w,[0,1,3,2]) w1 = w1[::-1,::-1,:,:] grad_y1 = np.transpose(grad_y,[1,2,0,3]) grad_x = Conv2D(grad_y,w1) grad_f = Conv2D(x1,grad_y1, pad = [I-1,J-1]) grad_w = np.transpose(grad_f,[1,2,0,3]) return grad_x, grad_w
def Conv2DBackward(grad_y, x, y, w): """Computes gradients of the convolutional layer. Args: grad_y: Gradients wrt. the inputs. x: Input values. y: Output values. Returns: grad_x: Gradients wrt. the inputs. grad_w: Gradients wrt. the weights. """ wshape = w.shape I = wshape[0] J = wshape[1] xcp = np.transpose(x,axes=[3,1,2,0]) grad_ycp = np.transpose(grad_y,axes=[1,2,0,3]) grad_w = Conv2D(xcp,grad_ycp,pad=(I-1,J-1)) # wcp = np.rot90(np.rot90(copy.deepcopy(w))) # flip k and c # transpose f wcp = np.zeros(wshape) for i in xrange(0,wshape[0]): for j in xrange(0,wshape[1]): wcp[i,j,:,:] = copy.deepcopy(w[I-i-1,J-j-1,:,:]) wcp = np.transpose(wcp, axes=[0,1,3,2]) grad_x = Conv2D(copy.deepcopy(grad_y),wcp, pad=(I-1,J-1)) return grad_x, np.transpose(grad_w, axes=[1,2,0,3])
def ConvMeanPool(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True, stride=1): output = Conv2D(name, input_dim, output_dim, filter_size, inputs, he_init=he_init, biases=biases, stride=stride) output = tf.add_n([ output[:, :, ::2, ::2], output[:, :, 1::2, ::2], output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2] ]) / 4. return output
def UpsampleConv(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True, stride=1): output = inputs output = tf.concat([output, output, output, output], axis=1) output = tf.transpose(output, [0, 2, 3, 1]) output = tf.depth_to_space(output, 2) output = tf.transpose(output, [0, 3, 1, 2]) output = Conv2D(name, input_dim, output_dim, filter_size, output, he_init=he_init, biases=biases, stride=stride) return output