神经网络适用于交叉熵,不适用于其他损失函数

朱利安

我将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

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

谢谢

朱利安

问题的解决方案在这里:TensorFlow,Julia //节点名称已存在于图中

必须使用TensorFlow的1.4.0版本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通用JS函数适用于某些div,不适用于其他div

子集不适用于某些数值,但适用于其他数值

MongoDB geoNear 不适用于某些坐标但适用于其他坐标

悬停格式仅适用于 $ 符号,不适用于其他货币?

通用排序适用于整数,但不适用于其他类型

@font-face 不适用于 chrome 但适用于其他浏览器

编码适用于 1 而不适用于 Twitter 中使用 python 的其他列表

背景图片仅适用于首页,而不适用于其他图片

媒体查询不适用于某些类的Bootstrap,但适用于其他类

代码适用于一种情况,但不适用于其他情况

.htaccess重定向仅适用于某些页面,而不适用于其他页面

AllowAnyMethod适用于某些API调用,但不适用于其他API调用

视差滚动适用于1张图像,但不适用于其他图像

Codeigniter文件上传许可仅适用于图像,不适用于其他图像

滚动功能适用于某些单击事件,但不适用于其他事件

代码适用于图像,但不适用于Codeigniter中的其他文件

方法重载不适用于其他参数

jQuery Enter键不适用于其他表单

apache2不适用于其他端口

静态库不适用于其他设备

窗口滚动不适用于其他情况

ActionPerformed方法不适用于其他类

FluentValidation不适用于其他库

ActionListener 不适用于其他类

SVG 线性渐变不适用于其他元素

字体不适用于网络

WiFi不适用于特定网络

什么激活函数适用于 Keras 回归神经网络的输入范围 (0,1) 和输出范围 (-∞,∞)

将熊猫列转换为适用于Keras神经网络的Numpy数组