卷积神经网络:权重和偏差初始化

时间:2018-11-06 23:55:04

标签: python tensorflow neural-network conv-neural-network multilabel-classification

我正在建立卷积神经网络以将数据分类为不同的类别 输入数据的形状为:30000,6,15,1 该数据有30000个样本,15个预测变量和6个可能的类别。

我的模型定义如下。

x = tf.placeholder("float", [None, 6,15,1])
y = tf.placeholder("float", [None, n_classes])

#Define Weights
weights = {
    'wc1': tf.get_variable('W0', shape=(3,3,1,8), initializer=tf.contrib.layers.xavier_initializer()), 
    'wc2': tf.get_variable('W1', shape=(3,3,32,12), initializer=tf.contrib.layers.xavier_initializer()), 
    'wc3': tf.get_variable('W2', shape=(3,3,64,16), initializer=tf.contrib.layers.xavier_initializer()), 
    'wc4': tf.get_variable('W3', shape=(3,3,64,20), initializer=tf.contrib.layers.xavier_initializer()),
    'wd1': tf.get_variable('W4', shape=(4*4*15,15), initializer=tf.contrib.layers.xavier_initializer()), 
    'out': tf.get_variable('W6', shape=(15,n_classes), initializer=tf.contrib.layers.xavier_initializer()), 
}

biases = {
    'bc1': tf.get_variable('B0', shape=(8), initializer=tf.contrib.layers.xavier_initializer()),
    'bc2': tf.get_variable('B1', shape=(12), initializer=tf.contrib.layers.xavier_initializer()),
    'bc3': tf.get_variable('B2', shape=(16), initializer=tf.contrib.layers.xavier_initializer()),
    'bc4': tf.get_variable('B3', shape=(20), initializer=tf.contrib.layers.xavier_initializer()),
    'bd1': tf.get_variable('B4', shape=(15), initializer=tf.contrib.layers.xavier_initializer()),
    'out': tf.get_variable('B5', shape=(6), initializer=tf.contrib.layers.xavier_initializer()),
}

#Define convolutional layer
def conv2d(x, W, b, strides=1, reuse=True):
    # Conv2D wrapper, with bias and relu activation
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)

#Define Maxpool layer
def maxpool2d(x, k=2):
    return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],padding='SAME')

#Define a convolutional neural network function
def conv_net(x, weights, biases):  
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    conv1 = maxpool2d(conv1, k=2)

    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    conv2 = maxpool2d(conv2, k=2)

    conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
    conv3 = maxpool2d(conv3, k=2)

    conv4 = conv2d(conv3, weights['wc4'], biases['bc4'])
    conv4 = maxpool2d(conv4, k=2)


    # Fully connected layer
    # Reshape conv2 output to fit fully connected layer input
    fc1 = tf.reshape(conv4, [-1, weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
    fc1 = tf.nn.relu(fc1)
    # Output, class prediction 
    out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
    return out

我收到错误消息:ValueError: Dimensions must be equal, but are 8 and 32 for 'Conv2D_1' (op: 'Conv2D') with input shapes: [?,8,3,8], [3,3,32,12].

执行时:

pred = conv_net(x, weights, biases)

我经历了多个conv2D模型,但是大多数模型都是用于图像分类的,在这里我可能会丢失一些我无法识别的东西。请帮忙。

1 个答案:

答案 0 :(得分:0)

权重 int mode = 1; Mode m = Mode.values()[mode - 1]; //m will be ONE wc2wc3的输入通道数必须与前一层的输出通道数相同。保持输出通道的数量不变,它们将更改为:

wc4