python - 在 TensorFlow 中使用 CNN 进行信号处理。类型错误 : DataType float32 for attr 'Tlabels' not in list of allowed values: int32, int64

标签 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)

最佳答案

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

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

关于python - 在 TensorFlow 中使用 CNN 进行信号处理。类型错误 : DataType float32 for attr 'Tlabels' not in list of allowed values: int32, int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737297/

相关文章:

python - 如何在 django View 中优化数据库查询集

python - 两个嵌套的 for 循环

Python 仅在文本文件中的某些位置执行操作

python - Theano 出现奇怪的类型错误

machine-learning - 神经网络标准化输出数据

Python 大字节数组内存

machine-learning - 训练 Keras Stateful LSTM return_seq=true 不学习

c - 使用相位累加器的 FM 合成

signal-processing - 高通DSP : hexagon-sim with command line arguments