weixin_41138872 2018-05-28 01:47 采纳率: 66.7%
浏览 999
已结题

sigmod两层训练模型怎样提取测试选择的结果

我做了两层神经网络模型,能够取出测试准确率但是取不出测试选则的分类结果。请问怎么提取?

##这些是训练环境
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

import pickle
from numpy import nan
import math

##这些是模型结构
n_hidden_1=170
n_hidden_2=128
n_input=214
n_classes=2
#inputs and outputs
x=tf.placeholder("float", [None,n_input])
y=tf.placeholder("float",[None,n_classes])
#network parameter
stddev=0.1
weights={
'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),
'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),
'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev))
}
biases={
'b1':tf.Variable(tf.random_normal([n_hidden_1])),
'b2':tf.Variable(tf.random_normal([n_hidden_2])),
'out':tf.Variable(tf.random_normal([n_classes]))
}
print("NETWORK READY")

###这些是训练方法
def multilayer_perceptron(_X,_weights,_biases):
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(_X,_weights['w1']),_biases['b1']))
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,_weights['w2']),_biases['b2']))
return(tf.matmul(layer_2,_weights['out'])+_biases['out'])

#prediction

pred=multilayer_perceptron(x,weights,biases)
#loss and opt
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))
optm=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)
corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accr=tf.reduce_mean(tf.cast(corr,"float"))

#Init
init=tf.global_variables_initializer()
print("Function")

training_lenth=len(G)####(回测长度)
training_epochs =400 #训练次数
batch_size = len(G) #每次迭代用多少样本(用全套)
display_step=4
#launch the graph
sess=tf.Session()
sess.run(init)

##这是训练和测试的过程
for epoch in range(training_epochs):
avg_cost=0.
total_batch=int(len(G)/batch_size)
#Iteration
for i in range(total_batch):
batch_xs=np.array(G[i])
batch_ys=np.array(F[i])
feeds={x:batch_xs,y:batch_ys}
sess.run(optm,feed_dict=feeds)
avg_cost+=sess.run(cost,feed_dict=feeds)
avg_cost=avg_cost/total_batch
#Display
if (epoch+1)%display_step==0:
print("Epoch:%03d/%03d cost:%.9f"%(epoch,training_epochs,avg_cost))
feeds={x:batch_xs, y:batch_ys}
train_acc=sess.run(accr, feed_dict=feeds)
print("Train accuracy:%.3f" % (train_acc))
feeds={x:G[i+1], y:F[i+1]}
test_acc=sess.run(accr, feed_dict=feeds)####这个是训练准确度
print("Test accuracy: %.3f" % (test_acc))
print("Optimization finished")

test_acc=sess.run(accr, feed_dict=feeds)我知道这个是训练准确度的提取方法
但是不知道训练结果选的分类都是什么,请问怎么得到训练结果?

  • 写回答

1条回答

  • jiangxiaoyu610 2018-07-30 13:41
    关注

    之前练习cnn的时候用过softmax,使用softmax时会运用以下代码,其中h_fc2是cnn中的全连接层。

     prediction = tf.nn.softmax(h_fc2, name='prob')
     ##########################################
     sess.run(prediction)
    

    sigmod应该也可以这样看吧,只不过输出的应该是分类的1或0

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)