I am trying to get a CNN to classify (two classes) exponential signals. For start I haven't split the data for train and validation, rather I am just trying to if I can train it or not.
I am having some difficulty understanding what are logits? are they the same thing as normalized data?
I have used this music genre classification and tried to see if I can adapt this model for my dataset. https://github.com/RobRomijnders/cnn_music/blob/master/CNN_music_main.py
I may be missing some parts of understanding, can anyone please help/suggest, where I am going wrong?
Here is the error I get after the 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
My code so far-
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.