ValueError:形状必须相等,但等级必须为3和0(将形状2与其他形状合并)

时间:2019-02-27 18:36:10

标签: python tensorflow

我正在通过从头开始编写代码来学习CNN。但是我遇到了以下错误:

ValueError:形状必须等于等级,但必须为3和0

将形状2与其他形状合并。对于具有输入形状:[],[5、3、8],[5、3、8],[]的“ conv1_1 / strided_slice_2 / stack_3”(操作:“ Pack”)。

这是我的代码(仅相关代码的一部分):

$.each(response, function (k,v){
    console.log('Position: '+k+'. Value: '+v);
});

似乎有问题:

#coding=utf-8
import tensorflow as tf      

def print_activations(t):                       
    print(t.op.name,'',t.get_shape().as_list)   
def get_weight(shape):                         
   return 
tf.Variable(tf.truncated_normal(shape,dtype=tf.float32,mean=0,stddev=1e- 1),name='weights') 

def my_conv2d(_input, _filter, _strides, padding='SAME'):  

    '''

    #input:[batch, in_height, in_width, in_channels]
    #filter:[filter_height, filter_width, in_channels, out_channels]         
    #Strides:[1, stride, stride, 1]
    #Padding:“SAME”or “VALID”
    '''
    inputShape = _input.get_shape().as_list()  
    filterShape = _filter.get_shape().as_list()   

    if padding=="SAME":                        
        NewHei = inputShape[1]+(filterShape[0]-1)  
        NewWid = inputShape[2]+(filterShape[1]-1)  
        offH = int((filterShape[0]-1)/2)      
        offW = int((filterShape[1]-1)/2) 

        heiAdd = tf.zeros([inputShape[0],offH, inputShape[2],inputShape[3]]) 
        weiAdd = tf.zeros([inputShape[0],inputShape[1]+2*offH, offW,inputShape[3]])   
        NewIn = tf.concat([heiAdd,_input,heiAdd],axis=1) 
        NewIn1 = tf.concat([weiAdd,NewIn,weiAdd],axis=2) 

        inputNewHei = NewHei 
        inputNewWid = NewWid 
        inputNew = NewIn1    
    else:                  
        inputNewHei = inputShape[1]    
        inputNewWid = inputShape[2]    
        inputNew = _input      

    out_height = int(((inputNewHei - filterShape[0]) / _strides[1]) + 1)  
    out_width = int(((inputNewWid - filterShape[0]) / _strides[1]) + 1)  

    temp = []                  
    for i in range(out_height): 
        for j in range(out_width):
            temp.append(inputNew[:, i*_strides[1]:(i*_strides[1]+ _filter[0]) , j*_strides[2]:(i*_strides[2]+ _filter[1]) , :])  

    temp1 = tf.stack(temp, axis=1)  

    tempIn = tf.reshape(temp1, [-1, filterShape[0]*filterShape[1]*filterShape[2]]) 
    tempFilter = tf.reshape(_filter, [-1, filterShape[3]]) 
    dst = tf.matmul(tempIn,tempFilter) 
    dst = tf.reshape(dst,[-1,out_height, out_width, filterShape[3]])
    return dst 

因为它出现在警告/错误窗口中。

有人可以帮我吗?非常感谢!

0 个答案:

没有答案
相关问题