0

你好

我用 TensorFlow 和 Julia 创建了一个代码来创建一个神经网络。

我有这个问题:我的网络在批量大小为 1 的情况下工作,并且在批量大小更大时不起作用......

这是我的代码:

您可以更改batch_sizebetween 1 和 10

ENV["CUDA_VISIBLE_DEVICES"] = "0" # It is to use the gpu
using TensorFlow
using Distributions

sess = Session(Graph())

batch_size = 10  # For this example, 0 < batch_size < 11
num_pixels = 64

###########

# Data base: 10 arrays, the first array is fill with 1, the second with 2 etc...

arrays = zeros(Float32,10,num_pixels,num_pixels)

arrays_2 = zeros(Float32,10)

for i in 1:10
    for j in 1:num_pixels
        for k in 1:num_pixels
            arrays[i,j,k] = i
            end
    end
end


for i in 1:10
    arrays_2[i] = i
end

###########

# inputs

x = placeholder(Float32, shape= [batch_size, 1, 1, 1])

y = placeholder(Float32)

###########

 # Function to create a batch

function create_batch(batch_size)
    x = zeros(Float32, batch_size,num_pixels, num_pixels)
    y = zeros(Float32, batch_size)

    for i in 1:batch_size
        x[i, : ,:] = arrays[i,:,:]

        y[i] = arrays_2[i]
    end
    y, x
end


###########

# Create the different layers ; poids = weight

variable_scope("mymodel" * randstring(), initializer=Normal(0, .001)) do
    global poids_1 = get_variable("p1", [2,2,2,1], Float32)
    global poids_2 = get_variable("p2",[4,4,3,2],Float32)
    global poids_3 = get_variable("p3",[2,2,4,3],Float32)
    global poids_4 = get_variable("p4",[1,4,4,4],Float32)
    global poids_5 = get_variable("p5",[1,4,4,4],Float32)
    global poids_6 = get_variable("p6",[1,4,4,4],Float32)
    global biases_1 = get_variable("b1",[2],Float32)
    global biases_2 = get_variable("b2",[3],Float32)
    global biases_3 = get_variable("b3",[4],Float32)
    global biases_4 = get_variable("b4",[4],Float32)
    global biases_5 = get_variable("b5",[4],Float32)
    global biases_6 = get_variable("b6",[4],Float32)
end

logits_1 = nn.relu(nn.conv2d_transpose(x, poids_1, [batch_size,2,2,2], [1,2,2,1],padding = "SAME") + biases_1)

logits_2 = nn.relu(nn.conv2d_transpose(logits_1,poids_2, [batch_size,4,4,3], [1,2,2,1],padding = "SAME") + biases_2)

logits_3 = nn.relu(nn.conv2d_transpose(logits_2,poids_3, [batch_size,8,8,4], [1,2,2,1],padding = "SAME") + biases_3)

logits_4 = nn.relu(nn.conv2d_transpose(logits_3,poids_4, [batch_size,16,16,4], [1,2,2,1],padding = "SAME") + biases_4)

logits_5 = nn.relu(nn.conv2d_transpose(logits_4,poids_5, [batch_size,32,32,4], [1,2,2,1],padding = "SAME") + biases_5)

 logits_6 = nn.relu(nn.conv2d_transpose(logits_5,poids_6, [batch_size,64,64,4], [1,2,2,1],padding = "SAME") + biases_6)

logits_6 = reduce_sum(logits_6, axis=[4])


if batch_size == 1

logits = reshape(logits_6, [num_pixels,num_pixels])

else

    logits = reshape(logits_6, [batch_size,num_pixels,num_pixels])

end


smax = nn.softmax(logits)



cross_entropy = reduce_mean(-reduce_sum(y.*log(smax))) # loss function

optimizer = train.AdamOptimizer(0.0001)

train_op = train.minimize(optimizer,cross_entropy)

batch = create_batch(batch_size)

run(sess, global_variables_initializer())

x_ = run(sess, train_op, Dict(x => reshape(batch[1], (batch_size,1,1,1)), y => reshape(batch[2], (batch_size,num_pixels,num_pixels))))

如果 batch_size 大于 1,则输入形状为以下错误[batch_size,num_pixels,num_pixels], [batch_size*num_pixels, 1]

    ERROR: LoadError: On worker 2:
Python error: PyObject ValueError(u"Dimensions must be equal, but are 64 and 640 for 'gradients/Softmax_grad/sub' (op: 'Sub') with input shapes: [10,64,64], [640,1].",)

如果 batch_size 为 1,我的 logits 必须是 2 维张量,但如果 batch_size 大于 1,我的 logits 必须是 3 维张量(将 batch_size 添加到维度)?

如何更改 batch_size ?

谢谢

4

1 回答 1

0

我找到了解决方案。

当 logits 被重塑时,使用以下重塑:

logits = reshape(logits_6, [batch_size,num_pixels*num_pixels])
于 2018-04-12T15:58:32.517 回答