【问题标题】:Neural network works with the cross entropy and does not with an other loss function神经网络与交叉熵一起工作,而不与其他损失函数一起工作
【发布时间】:2018-09-09 09:47:48
【问题描述】:

我正在使用 tensorflow 和 julia 来创建神经网络。

我可以创建一个带有 cross_entropy 损失函数的网络并且它可以工作:

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


function weight_variable(shape)
   initial = map(Float32, rand(Normal(0, .001), shape...))
   return Variable(initial)
end

function bias_variable(shape)
   initial = fill(Float32(.1), shape...)
   return Variable(initial)
end


sess = Session(Graph())

num_pixels = 12

num_classes = 10

x = placeholder((Float32), shape=[nothing, num_pixels])
y = placeholder(Float32, shape=[nothing, num_classes])

poids = weight_variable([num_pixels,num_classes]) # Weight

biases = bias_variable([num_classes])


cross_entropy = reduce_mean(-reduce_sum(y.*log(nn.softmax(x*poids + biases)))) # Cross entropy Loss function

optimizer = train.AdamOptimizer(0.0001)


train_op = train.minimize(optimizer,cross_entropy)

correct_prediction = equal(indmax(nn.softmax(x*poids + biases), 2), indmax(y,2))

accuracy = reduce_mean(cast(correct_prediction, Float32))


y1 = [0 0 1 0 0 0 0 0 0 0] # correct label

x1 = [0 0 0 5 6 3 2 0 0 0 0 0] # Input 

run(sess, global_variables_initializer())

for i in 1:10

x_ = run(sess,train_op,Dict(x => x1, y => y1))

acc = run(sess,accuracy,Dict(x => x1, y => y1))

info("train $i , accuracy = $acc")

end

close(sess)

现在,如果我只是用指数成本更改损失函数,如下所示:

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


function weight_variable(shape)
   initial = map(Float32, rand(Normal(0, .001), shape...))
   return Variable(initial)
end

function bias_variable(shape)
   initial = fill(Float32(.1), shape...)
   return Variable(initial)
end


sess = Session(Graph())

num_pixels = 12

num_classes = 10

x = placeholder((Float32), shape=[nothing, num_pixels])
y = placeholder(Float32, shape=[nothing, num_classes])

poids = weight_variable([num_pixels,num_classes]) # Weight

biases = bias_variable([num_classes])

expo = reduce_mean((0.5*exp((1/0.5).*reduce_sum((nn.softmax(x*poids + biases)- y)^2)))) # Exponential loss function

optimizer = train.AdamOptimizer(0.0001)


train_op = train.minimize(optimizer,expo)

correct_prediction = equal(indmax(nn.softmax(x*poids + biases), 2), indmax(y,2))

accuracy = reduce_mean(cast(correct_prediction, Float32))


y1 = [0 0 1 0 0 0 0 0 0 0] # correct label

x1 = [0 0 0 5 6 3 2 0 0 0 0 0] # Input 

run(sess, global_variables_initializer())

for i in 1:10

x_ = run(sess,train_op,Dict(x => x1, y => y1))

acc = run(sess,accuracy,Dict(x => x1, y => y1))

info("train $i , accuracy = $acc")

end

close(sess)

它不起作用,我有以下错误:

ERROR: LoadError: Tensorflow error: Status: Node name 'gradients/Softmax_grad/Sum' already exists in the Graph

Stacktrace:
 [1] (::Atom.##110#114{String,String})() at /home/jabou/.julia/v0.6/Atom/src/eval.jl:104
 [2] withpath(::Atom.##110#114{String,String}, ::String) at /home/jabou/.julia/v0.6/CodeTools/src/utils.jl:30
 [3] withpath(::Function, ::String) at /home/jabou/.julia/v0.6/Atom/src/eval.jl:38
 [4] hideprompt(::Atom.##109#113{String,String}) at /home/jabou/.julia/v0.6/Atom/src/repl.jl:66
 [5] macro expansion at /home/jabou/.julia/v0.6/Atom/src/eval.jl:99 [inlined]
 [6] (::Atom.##108#112{Dict{String,Any}})() at ./task.jl:80
while loading /home/jabou/Bureau/Minimum nouveau.jl, in expression starting on line 37

我不明白为什么...你能帮帮我吗?

谢谢

【问题讨论】:

  • 我也觉得很奇怪。尝试将 name=... 添加到损失函数中的所有这些张量中。
  • 感谢您的评论,但它不起作用...仍然是相同的错误消息。

标签: tensorflow neural-network julia loss-function


【解决方案1】:

在 Tensorflow.jl 中,我通常使用具有随机名称 ("mymodel" * randstring()) 的变量范围,这样当您运行代码两次(例如在交互式会话中)时,不会出现命名冲突。

variable_scope("mymodel" * randstring(), initializer=Normal(0, .1)) do
    global w1 = get_variable("weights1", [num_input, hidden_units1], Float32)
    global b1 = get_variable("b1",[hidden_units1],Float32)
    # other parameters ....
end

这有帮助吗?

【讨论】:

  • 不,它仍然不起作用。我有同样的错误信息。唯一的区别是交叉熵的准确性:现在是 0...第一次运行代码时会出现此错误,而不仅仅是运行两次时。
  • 如果我使用的是 nn.relu,而不是 nn.softmax,它可以工作......所以,问题来自 softmax 吗?
【解决方案2】:

问题的解决方案在这里:TensorFlow, Julia // Node name already exists in the Graph

必须使用 TensorFlow 1.4.0 版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2018-09-21
    • 2017-12-24
    相关资源
    最近更新 更多