【发布时间】:2016-08-03 07:38:23
【问题描述】:
我正在尝试让 CNN 对(两类)指数信号进行分类。首先,我没有将数据拆分为训练和验证,而我只是在尝试是否可以训练它。
我很难理解什么是 logits?它们和标准化数据是一样的吗?
我已经使用了这个音乐流派分类,并尝试看看我是否可以为我的数据集调整这个模型。 https://github.com/RobRomijnders/cnn_music/blob/master/CNN_music_main.py
我可能缺少一些理解,任何人都可以帮助/建议,我哪里出错了?
这是我在 step loss = tf.nn.sparse_softmax_cross_entropy_with_logits(h_fc2,y_)-
Traceback (most recent call last):
File "/home/raisa/PycharmProjects/NN_model/patterns.py", line 96, in <module>
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(h_fc2,y_)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 265, in sparse_softmax_cross_entropy_with_logits
logits, labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 962, in _sparse_softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 486, in apply_op
_Attr(op_def, input_arg.type_attr))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 59, in _SatisfiesTypeConstraint
", ".join(dtypes.as_dtype(x).name for x in allowed_list)))
TypeError: DataType float32 for attr 'Tlabels' not in list of allowed values: int32, int64
Process finished with exit code 1
到目前为止我的代码-
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tensorflow.python.framework import ops
from tensorflow.python.ops import clip_ops
from bnf import *
#hyperparameters
Batch_size= 100
max_iteration= 50
learning_rate=1000
filt_1= [10,1,1]
num_fc_1 = 10
dropout = 0.5
num_classes = 2
#training data
lorange= 1
hirange= 15
amplitude= 10
t= 10
random.seed()
tau=np.random.uniform(lorange,hirange)
def generate_data(randomsignal):
X= np.arange(t)
Y= amplitude*np.exp(-X/tauA)
return X, Y
#tensors for input data
X= tf.placeholder(tf.float32, shape= [None, 10])
y_= tf.placeholder(tf.float32, shape= [None])
Y_class= tf.argmax(y_, dimension=1)
bn_train = tf.placeholder(tf.bool)
keep_prob = tf.placeholder('float', name = 'dropout_keep_prob')
def weight_variable(shape, name):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial, name = name)
def bias_variable(shape, name):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial, name = name)
def conv2d(X, W):
return tf.nn.conv2d(X, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
with tf.name_scope("Reshaping_data") as scope:
X_node = tf.reshape(X, [-1,2,1,1])
with tf.name_scope("Conv1") as scope:
W_conv1 = weight_variable([filt_1[1], 1, 1, filt_1[0]], 'Conv_Layer_1')
b_conv1 = bias_variable([filt_1[0]], 'bias_for_Conv_Layer_1')
a_conv1 = conv2d(X_node, W_conv1) + b_conv1
h_conv1 = tf.nn.relu(a_conv1)
with tf.name_scope('max_pool1') as scope:
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, filt_1[2], 1, 1],
strides=[1, filt_1[2], 1, 1], padding='VALID')
width_pool1 = int(np.floor((10-filt_1[2])/filt_1[2]))+1
size1 = tf.shape(h_pool1)
with tf.name_scope('Batch_norm1') as scope:
a_bn1 = batch_norm(h_pool1,filt_1[0],bn_train,'bn')
h_bn1 = tf.nn.relu(a_bn1)
with tf.name_scope("Fully_Connected1") as scope:
W_fc1 = weight_variable([width_pool1 * filt_1[0], num_fc_1], 'Fully_Connected_layer_1')
b_fc1 = bias_variable([num_fc_1], 'bias_for_Fully_Connected_Layer_1')
h_flat = tf.reshape(h_bn1, [-1, width_pool1 * filt_1[0]])
h_flat = tf.nn.dropout(h_flat, keep_prob)
h_fc1 = tf.nn.relu(tf.matmul(h_flat, W_fc1) + b_fc1)
with tf.name_scope("Output_layer") as scope:
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = tf.Variable(tf.truncated_normal([num_fc_1, num_classes], stddev=0.1),name = 'W_fc2')
b_fc2 = tf.Variable(tf.constant(0.1, shape=[num_classes]),name = 'b_fc2')
h_fc2 = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
size3 = tf.shape(h_fc2)
with tf.name_scope("SoftMax") as scope:
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(h_fc2,y_)
cost = tf.reduce_sum(loss) / batch_size
loss_summ = tf.scalar_summary("cross entropy_loss", cost)
【问题讨论】:
-
请edit您的问题并包含完整的回溯,或者至少指定导致错误的代码行。
-
您好我已经编辑了问题并添加了回溯。基本上这个错误发生在我尝试计算 softmax 和交叉熵损失 = tf.nn.sparse_softmax_cross_entropy_with_logits(h_fc2,y_)
-
错误是说你传入的东西有 32 位浮点值但只有 32 或 64 位整数是可以接受的。我怀疑这是因为
y_= tf.placeholder(tf.float32, shape= [None])行,所以尝试将第一个参数更改为tf.int32。如果不希望这样做,请尝试在调用之前将所有float32值转换为后者。 -
我尝试将占位符更改为 -
X= tf.placeholder(tf.int32, shape= [None, 10])y_= tf.placeholder(tf.int32, shape= [None])但现在错误是 TypeError: DataType int32 for attr 'T' not in list of allowed values: float32, float64 -
好吧,那么就按照我的建议只更改
y_的那个...不要理会X。
标签: python neural-network signal-processing deep-learning conv-neural-network