在张量流中使用CNN的信号处理。 TypeError:attr'Tlabels'的DataType float32不在允许值列表中:int32,int64

时间:2016-08-03 07:38:24

标签: python neural-network signal-processing deep-learning conv-neural-network

我试图让CNN对(两类)指数信号进行分类。首先,我没有拆分列车和验证的数据,而是我只是想尝试,如果我可以训练它。

我在理解什么是logits时遇到了一些困难?它们与标准化数据相同吗?

我已经使用了这种音乐类型分类,并试图看看我是否可以为我的数据集调整此模型。  https://github.com/RobRomijnders/cnn_music/blob/master/CNN_music_main.py

我可能会遗漏某些理解部分,任何人都可以帮助/建议,我哪里出错了?

以下是步骤丢失后得到的错误= 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)

1 个答案:

答案 0 :(得分:1)

您的真实标签必须是int32或int64格式,因此请将y_更改为

y_= tf.placeholder(tf.int32, shape= [None])