Tensorflow: cost not changing - tensorflow

I build up a CNN in the following structure. The inputs are 32 * 32 * 3 pictures and a one-hot label of 10 categories.
inputs = tf.placeholder(tf.float32, [None, 32, 32, 3], name = "input")
targets = tf.placeholder(tf.float32, [None, 10], name = "targets")
layer_1_filter = tf.layers.conv2d(inputs = inputs,
filters = 64,
kernel_size = (2, 2),
strides = (1, 1),
padding = "same",
activation= tf.nn.relu)
layer_2_pooling = tf.layers.max_pooling2d(inputs = layer_1_filter,
pool_size = (2 * 2),
strides =1 * 1,
padding = 'same')
layer_3_filter = tf.layers.conv2d(inputs = layer_2_pooling,
filters = 128,
kernel_size = (4, 4),
strides = (1, 1),
padding = "same",
activation= tf.nn.relu)
layer_4_pooling = tf.layers.max_pooling2d(inputs = layer_3_filter,
pool_size = (2 * 2),
strides = 1 * 1,
padding = 'same')
sha = np.prod(layer_4_pooling.get_shape().as_list()[1:])
layer_5_reshape = tf.reshape(tensor= layer_4_pooling,
shape = [-1, sha])
layer_6_fc = tf.contrib.layers.fully_connected(inputs = layer_5_reshape,
num_outputs = 1024)
layer_6_fc = tf.nn.dropout(layer_6_fc, keep_prob) # Faster with drop out
layer_7_fc2 = tf.contrib.layers.fully_connected(inputs = layer_6_fc,
num_outputs = 512)
layer_8_fc3 = tf.contrib.layers.fully_connected(inputs = layer_7_fc2,
num_outputs = 10)
layer_9_logit = tf.identity(input = layer_8_fc3,
name = "logistic")
And I define my cost and optimizer as:
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=layer_9_logit, labels=targets))
optimizer = tf.train.AdamOptimizer().minimize(cost)
When I run it, the cost is always around a certain number: 2.30. I tried it several times and it always converge to it.
count = 0
with tf.Session() as sess:
print(info)
sess.run(tf.global_variables_initializer())
for batch_i in range(img_shape[0] // batch_size - 1):
feature_batch = picture[batch_i * batch_size: (batch_i + 1) * batch_size]
label_batch = label[batch_i * batch_size: (batch_i + 1) * batch_size]
train_loss, _ = sess.run([cost, optimizer],
feed_dict={inputs: feature_batch,
targets: label_batch})
if (count % 10 == 0):
print(str(count) + ' | Train Loss {:.8f}'.format(train_loss))
count += 1
output
0 | Train Loss 37.51004410
10 | Train Loss 2.30226469
20 | Train Loss 2.30263376
30 | Train Loss 2.30258608
40 | Train Loss 2.30258536
50 | Train Loss 2.30265045
60 | Train Loss 2.35271192
70 | Train Loss 2.30241871
May I ask why and how to fix it? Thanks a lot

Related

Tensorflow 1 to Tensorflow 2

I have tried to write tensorflow 1 model in tensorflow 2 using keras api. Why the losses I am getting differ a lot? Aren't they the same models?
TF1 version:
conv1 = tf.compat.v1.layers.conv1d(input_data, num_filters, 1, strides=1, padding = 'valid', kernel_initializer=tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros')
conv1_norm = tf.compat.v1.layers.batch_normalization(conv1)
conv1_activ = tf.nn.sigmoid(conv1_norm)
conv2 = tf.compat.v1.layers.conv1d(conv1_activ, 2*num_filters, 3, padding = 'valid', kernel_initializer=tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros')
conv2_norm = tf.compat.v1.layers.batch_normalization(conv2)
conv2_activ = tf.nn.sigmoid(conv2_norm)
dropout = tf.nn.dropout(conv2_activ, rate = 1 - (1 - dropout_prob/2))
flat_layer = tf.compat.v1.layers.flatten(dropout)
dense = tf.compat.v1.layers.dense(flat_layer, units = 4 * num_filters, kernel_initializer=tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros')
dense_norm = tf.compat.v1.layers.batch_normalization(dense)
dense_activ = tf.nn.sigmoid(dense_norm)
y_pred = tf.compat.v1.layers.dense(dense_activ, units = 1, kernel_initializer=tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros')
y_pred = tf.nn.tanh(y_pred)
Phi_t_predict = Phi0_t_tf + y_pred
AI_t_predict = 5 * Phi_t_predict + 0.3
ref_t_predict = (AI_t_predict[1:] - AI_t_predict[:-1]) / (AI_t_predict[:-1] + AI_t_predict[1:])
syn_PP_t = tf.matmul(wvlt_map_tf, ref_t_predict)
data_misfit = 100 * tf.reduce_mean(input_tensor=tf.square(syn_PP_t - output_data))
model_misfit = 1 * tf.reduce_mean(input_tensor=tf.square(Phi_t_predict - Phi0_t_tf))
up = tf.reduce_mean(input_tensor=tf.square(Phi_t_predict[0] - Phi0_t_tf[0]))
bottom = tf.reduce_mean(input_tensor=tf.square(Phi_t_predict[-1] - Phi0_t_tf[-1]))
bound = 1 * (up + bottom) / 2
loss = data_misfit + model_misfit
optimizer = tf.compat.v1.train.AdamOptimizer(0.001)
train = optimizer.minimize(loss)
#%%
plt.close('all')
iter = 5001
l = np.zeros((iter,1))
data_error = np.zeros((iter,1))
model_error = np.zeros((iter,1))
AI_predict_iter = np.zeros((len(AI0_t),iter))
Phi_predict_iter = np.zeros((len(AI0_t),iter))
with tf.compat.v1.Session() as sess:
sess.run(tf.compat.v1.global_variables_initializer())
for step in range(iter):
dic = {input_data: X_train, output_data: Y_train}
_, AI_predict, l[step], model_error[step], Phi_predict, contrast = sess.run([train, AI_t_predict, loss, model_misfit, Phi_t_predict, y_pred], feed_dict = dic)
ref_predict = (AI_predict[1:] - AI_predict[:-1]) / (AI_predict[:-1] + AI_predict[1:])
top = AI_predict[0] - AI0_t[0]
bottom = AI_predict[-1] - AI0_t[-1]
AI_predict = AI_predict - (top + bottom) / 2
AI_predict_iter[:,step] = AI_predict.ravel()
top = Phi_predict[0] - Phi0_t[0]
bottom = Phi_predict[-1] - Phi0_t[-1]
Phi_predict = Phi_predict - (top + bottom) / 2
Phi_predict_iter[:,step] = Phi_predict.ravel()
Tensorflow 2 model:
model = keras.Sequential()
model.add(layers.Conv1D(filters = num_filters, kernel_size=1, strides=1, padding = 'valid', kernel_initializer = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros'))
model.add(layers.BatchNormalization())
model.add(layers.Activation('sigmoid'))
model.add(layers.Conv1D(filters = num_filters, kernel_size = 3, padding = 'valid', kernel_initializer = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer='zeros'))
model.add(layers.BatchNormalization())
model.add(layers.Activation('sigmoid'))
model.add(layers.Dropout(rate = 0.1))
model.add(layers.Flatten())
model.add(layers.Dense(units = 4 * num_filters, kernel_initializer = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer = 'zeros'))
model.add(layers.BatchNormalization())
model.add(layers.Activation('sigmoid'))
model.add(layers.Dense(units = 1, kernel_initializer = tf.compat.v1.keras.initializers.VarianceScaling(scale=1.0, mode="fan_avg", distribution="uniform"), bias_initializer = 'zeros'))
model.add(layers.Activation('tanh'))
def loss_fn(y_pred, y_true):
Phi_t_predict = Phi0_t_tf + y_pred
AI_t_predict = 5 * Phi_t_predict + 0.3
ref_t_predict = (AI_t_predict[1:] - AI_t_predict[:-1]) / (AI_t_predict[:-1] + AI_t_predict[1:])
syn_PP_t = tf.matmul(wvlt_map_tf, ref_t_predict)
y_true = y_true[1:,]
data_misfit = 100 * tf.reduce_mean(input_tensor=tf.square(syn_PP_t - y_true))
model_misfit = 1 * tf.reduce_mean(input_tensor=tf.square(Phi_t_predict - Phi0_t_tf))
up = tf.reduce_mean(input_tensor=tf.square(Phi_t_predict[0] - Phi0_t_tf[0]))
bottom = tf.reduce_mean(input_tensor=tf.square(Phi_t_predict[-1] - Phi0_t_tf[-1]))
bound = 1 * (up + bottom) / 2
loss = data_misfit + model_misfit
return loss
model.compile(loss=loss_fn, optimizer=keras.optimizers.Adam(learning_rate=0.001))
model.fit(X_train, Y_train, epochs=5001, batch_size=64)
Tensorflow has upgrading feature and using it I made tf1 written model executable in tf2, however I need it to write in tf2 with keras

tensorflow improper batch setup Check failed: cudnnSetTensorNdDescriptor when using upsampling

I think I am setting up my batches wrong. If I run with the generated dataset it runs fine but with my own data I get an error.
If I take out the encoder (max pooling) and decoder (UpSampling2D) I don't get an error.
input size (304, 228, 1)
Generated: RUNS
import tensorflow as tf
from tensorflow.keras import layers
from natsort import natsorted
from tensorflow.keras.models import Model
BATCH_SIZE = 4
EPOCHS = 20
LEARNING_RATE = 1e-4
RESET_TRAINING = True
INPUT_CHANNELS = 1
OUTPUT_CHANNELS = 1
LOSS_TYPE = tf.keras.losses.SparseCategoricalCrossentropy()
img_size = (304, 228)
# configure cuda
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
class UnetModel(Model):
def __init__(self, *args, **kwargs):
super().__init__(UnetModel, *args, **kwargs)
# -- Encoder -- #
# Block encoder 1
input_shape = (img_size[0], img_size[1], 1)
# If you want to know more about why we are using `he_normal`:
# https://stats.stackexchange.com/questions/319323/whats-the-difference-between-variance-scaling-initializer-and-xavier-initialize/319849#319849
# Or the excellent fastai course:
# https://github.com/fastai/course-v3/blob/master/nbs/dl2/02b_initializing.ipynb
initializer = 'he_normal'
inputs = layers.Input(shape=input_shape)
print("input shape ", input_shape)
conv_enc_1 = layers.Conv2D(64, 3, activation='relu', padding='same', kernel_initializer=initializer)(inputs)
conv_enc_1 = layers.Conv2D(64, 3, activation = 'relu', padding='same', kernel_initializer=initializer)(conv_enc_1)
# Block encoder 2
max_pool_enc_2 = layers.MaxPooling2D(pool_size=(2, 2))(conv_enc_1)
conv_enc_2 = layers.Conv2D(128, 5, activation = 'relu', padding = 'same', kernel_initializer = initializer)(max_pool_enc_2)
conv_enc_2 = layers.Conv2D(128, 5, activation = 'relu', padding = 'same', kernel_initializer = initializer)(conv_enc_2)
# Block decoder 1
up_dec_4 = layers.Conv2D(64, 2, activation = 'relu', padding = 'same', kernel_initializer = initializer)(layers.UpSampling2D(size = (2,2))(conv_enc_2))
merge_dec_4 = layers.concatenate([conv_enc_1, up_dec_4], axis = 3)
conv_dec_4 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = initializer)(merge_dec_4)
conv_dec_4 = layers.Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = initializer)(conv_dec_4)
conv_dec_4 = layers.Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = initializer)(conv_dec_4)
# -- Dencoder -- #
output = layers.Conv2D(1, 1, activation = 'softmax')(conv_dec_4)
self.model = tf.keras.Model(inputs = inputs, outputs = output)
def call(self, x):
return self.model(x)
model = UnetModel()
model.compile(optimizer=tf.keras.optimizers.Adam(LEARNING_RATE), loss = LOSS_TYPE, metrics= [tf.keras.metrics.get('accuracy')])
dataset_debug = tf.data.Dataset.from_tensor_slices((tf.random.normal(shape = (BATCH_SIZE, img_size[0], img_size[1], 1)), tf.random.normal(shape = (BATCH_SIZE, img_size[0], img_size[1], 1)))).batch(BATCH_SIZE)
history = model.fit(dataset_debug, epochs=EPOCHS, shuffle=True)
Does NOT run
Here I am splitting the filenames into training and validation sets using train_test_split and reading in images in the parse_img_input function
# takes image filenames of uint8 and normalizes to 0-1 range
def parse_img_input(img_file, img_file_out):
print("img file ", img_file)
def _parse_input(img_file, img_file_out):
# get img image
d_filepath = img_file.numpy().decode()
d_image_decoded = tf.image.decode_jpeg(
tf.io.read_file(d_filepath), channels=1)
d_image = tf.cast(d_image_decoded, tf.float32) / 255.0
# get img image
d_filepath_out = img_file_out.numpy().decode()
d_image_decoded_out = tf.image.decode_jpeg(
tf.io.read_file(d_filepath_out), channels=1)
d_image_out = tf.cast(d_image_decoded_out, tf.float32) / 255.0
# add channel dimension
d_image = tf.expand_dims(d_image, -1)
d_image_out = tf.expand_dims(d_image_out, -1)
return d_image, d_image_out
return tf.py_function(_parse_input,
inp=[img_file, img_file_out],
Tout=[tf.float32, tf.float32])
# depth_files_in, depth_files_out are lists of filenames
# split input data into train, test sets
X_train_file, X_test_file, y_train_file, y_test_file = train_test_split(depth_files_in, depth_files_out,
test_size=0.2,
random_state=0)
dataset_train = tf.data.Dataset.from_tensor_slices((X_train_file, y_train_file))
dataset_train = dataset_train.map(parse_img_input)
dataset_test = tf.data.Dataset.from_tensor_slices((X_test_file, y_test_file))
dataset_test = dataset_test.map(parse_img_input)
history = model.fit(dataset_train, epochs=EPOCHS, shuffle=True, batch_size = BATCH_SIZE, validation_data= dataset_test)
F tensorflow/stream_executor/cuda/cuda_dnn.cc:535] Check failed: cudnnSetTensorNdDescriptor(handle_.get(), elem_type, nd, dims.data(), strides.data()) == CUDNN_STATUS_SUCCESS (3 vs. 0)batch_descriptor: {count: 228 feature_map_count: 64 spatial: 152 0 value_min: 0.000000 value_max: 0.000000 layout: BatchDepthYX}

TensorFlow equivalent of this code written in Keras

The Keras code runs perfectly, and the loss is close to zero. The input data is xData, the labeled data is yTrainData.
xData = np.reshape(xData, (-1, 1, sendLengthG * 4))
yTrainData = np.reshape(yTrainData, (-1, sendLengthG, sentComponentTypeCount))
model = k.models.Sequential()
model.add(k.layers.Dense(512, input_shape=(1, sendLengthG * 4), activation='tanh'))
model.add(k.layers.Dense(sendLengthG * sentComponentTypeCount, activation='linear'))
model.add(k.layers.Reshape([sendLengthG, sentComponentTypeCount]))
model.add(k.layers.Dense(sentComponentTypeCount, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='RMSProp', metrics=['accuracy'])
model.summary()
model.fit(xData, yTrainData, epochs=roundCount, batch_size=1, verbose=2)
I've written some TensorFlow code like below, but cannot reduce the loss under 0.011
x = tf.placeholder(dtype=tf.float32)
yTrain = tf.placeholder(dtype=tf.float32)
x1 = tf.reshape(x, shape=[1, sendLengthG * 4])
nodeCount1 = 18
w1 = tf.Variable(tf.random_normal([sendLengthG * 4, nodeCount1], mean=0.5, stddev=0.1), dtype=tf.float32)
b1 = tf.Variable(tf.zeros([nodeCount1]), dtype=tf.float32)
n1 = tf.nn.tanh(tf.matmul(x1, w1) + b1)
nodeCount2 = 21
w2 = tf.Variable(tf.random_normal([nodeCount1, nodeCount2], mean=1.5, stddev=0.1), dtype=tf.float32)
b2 = tf.Variable(tf.zeros([nodeCount2]), dtype=tf.float32)
n2 = tf.nn.tanh(tf.matmul(n1, w2) + b2)
wn = tf.Variable(tf.random_normal([nodeCount2, sendLengthG * sentComponentTypeCount], mean=0.5, stddev=0.1), dtype=tf.float32)
bn = tf.Variable(tf.zeros([sendLengthG * sentComponentTypeCount]), dtype=tf.float32)
y = tf.matmul(n2, wn) + bn
yResult = tf.nn.softmax(tf.reshape(y, [sendLengthG, -1]))
loss = -tf.reduce_mean(yTrain * tf.log(tf.clip_by_value(yResult, 1e-10, 1.0)))
optimizer = tf.train.RMSPropOptimizer(learnRate)
train = optimizer.minimize(loss)

Getting different Accuracy rates in convolutional neural networks while using tensorflow and tflearn

So i was using tflearn to make a CNN and the accuracy was nice, but i tried to train the same kind of network with the same learning rates and other parameters. But for some reason i don't understand the accuracy i got when using tensorflow was lower. Is there any reason that this happened?
Here is my Neural net layer:
def cnn(x):
x = tflearn.layers.core.input_data(shape=[None, 50, 50, 3], placeholder=x)
conv_layer1 = tflearn.layers.conv.conv_2d(x, nb_filter=32, filter_size=5, activation='relu')
out_layer_1 = tflearn.layers.max_pool_2d(conv_layer1, 5)
conv_layer2 = tflearn.layers.conv.conv_2d(out_layer_1, nb_filter=64, filter_size=5, activation='relu')
out_layer_2 = tflearn.layers.max_pool_2d(conv_layer2, 5)
conv_layer3 = tflearn.layers.conv.conv_2d(out_layer_2, nb_filter=128, filter_size=5, activation='relu')
out_layer_3 = tflearn.layers.max_pool_2d(conv_layer3, 5)
conv_layer4 = tflearn.layers.conv.conv_2d(out_layer_3, nb_filter=64, filter_size=5, activation='relu')
out_layer_4 = tflearn.layers.max_pool_2d(conv_layer4, 5)
conv_layer5 = tflearn.layers.conv.conv_2d(out_layer_4, nb_filter=32, filter_size=5, activation='relu')
out_layer_5 = tflearn.layers.max_pool_2d(conv_layer5, 5)
fc1 = tflearn.layers.core.fully_connected(out_layer_5, 1024, activation='relu', name="FC1")
fc1_dropout = tflearn.layers.core.dropout(fc1, 0.5)
output = tflearn.layers.core.fully_connected(fc1_dropout, 2, activation='softmax', name='output')
return output
And here is my training function:
def train_model():
x = tf.placeholder(tf.float32, shape=[None, 50, 50, 3], name="x")
x_image = tf.reshape(x, [-1, 50, 50, 3])
y = tf.placeholder(tf.float32, shape=[None, 2], name="y")
y_cls = tf.argmax(y, dimension=1)
y_pred = cnn(x_image)
print "Importing Training Data..."
x_train = np.load('data/CatOrDog/training_images.npy')
y_train = np.load('data/CatOrDog/training_labels.npy')
y_train = [[1, 0] if label == 'Dog' else [0, 1] for label in y_train]
y_train = np.array(y_train)
randomer = np.arange(x_train.shape[0])
np.random.shuffle(randomer)
x_train = x_train[randomer]
y_train = y_train[randomer]
n_data = len(x_train)
x_train = np.array(x_train, dtype='float32')
print "Images Shape: ", x_train.shape, "\t", x_train.dtype
print "\nImporting Testing Data..."
x_test = np.load('data/CatOrDog/testing_images.npy')
y_test = np.load('data/CatOrDog/testing_labels.npy')
y_test = [[1, 0] if testing_label == 'Dog' else [0, 1] for testing_label in y_test]
y_test = np.array(y_test)
x_test = np.array(x_test, dtype='float32')
randomer = np.arange(x_test.shape[0])
np.random.shuffle(randomer)
x_test = x_test[randomer]
y_test = y_test[randomer]
n_data = len(x_train)
n_test_data =len(x_test)
'''divider = int(n_test_data / 2)
x_test_data = x_test[0:divider]
y_test_data = y_test[0:divider]
x_validation_data = x_test[divider+1:n_test_data-1]
y_validation_data = y_test[divider + 1:n_test_data - 1]'''
with tf.variable_scope("Softmax"):
y_pred_cls = tf.argmax(y_pred, dimension=1)
with tf.name_scope("cross_ent"):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_pred, labels=y)
cost = tf.reduce_mean(cross_entropy)
with tf.name_scope("Optimizer"):
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
with tf.name_scope("Accuracy"):
correct_prediction = tf.equal(y_pred_cls, y_cls)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
writer = tf.summary.FileWriter("Training_FileWriter/")
writer1 = tf.summary.FileWriter("Validation_FileWriter/")
tf.summary.scalar('loss', cost)
tf.summary.scalar('accuracy', accuracy)
merged_summary = tf.summary.merge_all()
num_epochs = 10
batch_size = 300
with tf.Session() as sess:
# x = sess.graph.get_tensor_by_name('x')
sess.run(tf.global_variables_initializer())
writer.add_graph(sess.graph)
for epoch in range(num_epochs):
start_time = time.time()
train_accuracy = 0
cur_batch = int(n_data / batch_size)
prev_index = 0
bar = progressbar.ProgressBar(maxval=cur_batch)
bar.start()
for batch in range(0, cur_batch):
start, end = ClassifyData.get_batch_array_indexes(previous_index=prev_index, batch_size=batch_size, n_data=n_data)
if start == n_data:
break
x_batch = x_train[start:end]
y_true_batch = y_train[start:end]
feed_dict_train = {x: x_batch, y: y_true_batch}
sess.run(optimizer, feed_dict=feed_dict_train)
train_accuracy += sess.run(accuracy, feed_dict=feed_dict_train)
summ = sess.run(merged_summary, feed_dict=feed_dict_train)
writer.add_summary(summ, epoch * int(n_data / batch_size) + batch)
bar.update(batch)
bar.finish()
train_accuracy /= int(n_data / batch_size)
summ, vali_accuracy = sess.run([merged_summary, accuracy],
feed_dict={x: x_test, y: y_test})
writer1.add_summary(summ, epoch)
end_time = time.time()
print "\nEpoch " + str(epoch + 1) + " completed : Time usage " + str(
int(end_time - start_time)) + " seconds"
print "\tAccuracy:"
print "\t- Training Accuracy:\t{}".format(train_accuracy)
print "\t- Validation Accuracy:\t{}".format(vali_accuracy)
PS I am using tflearn to build my network.

ValueError: Cannot feed value of shape (3375, 50, 50, 2) for Tensor 'Reshape:0', which has shape '(?, 5000)'

I am learning Tensorflow. Following is my code for MLP with TensorFlow. I have some issues with mismatching of data dimentions.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
wholedataset = np.load('C:/Users/pourya/Downloads/WholeTrueData.npz')
data = wholedataset['wholedata'].astype('float32')
label = wholedataset['wholelabel'].astype('float32')
height = wholedataset['wholeheight'].astype('float32')
print(type(data[20,1,1,0]))
learning_rate = 0.001
training_iters = 5
display_step = 20
n_input = 3375
X = tf.placeholder("float32")
Y = tf.placeholder("float32")
weights = {
'wc1': tf.Variable(tf.random_normal([3, 3, 2, 1])),
'wd1': tf.Variable(tf.random_normal([3, 3, 1, 1]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([1])),
'out': tf.Variable(tf.random_normal([1,50,50,1]))
}
mnist= data
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes = 2
batch_size = 100
x = tf.placeholder('float', shape = [None,50,50,2])
shape = x.get_shape().as_list()
dim = np.prod(shape[1:])
x_reshaped = tf.reshape(x, [-1, dim])
y = tf.placeholder('float', shape= [None,50,50,2])
shape = y.get_shape().as_list()
dim = np.prod(shape[1:])
y_reshaped = tf.reshape(y, [-1, dim])
def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([5000,
n_nodes_hl1])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,
n_nodes_hl2])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,
n_nodes_hl3])),
'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3,
n_classes])),
'biases':tf.Variable(tf.random_normal([n_classes])),}
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']),
hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']),
hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']),
hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(n_input/batch_size)):
epoch_x = wholedataset['wholedata'].astype('float32')
epoch_y = wholedataset['wholedata'].astype('float32')
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y:
epoch_y})
epoch_loss += c
print('Epoch', epoch, 'completed out
of',hm_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:mnist.test.images,
y:mnist.test.labels}))
train_neural_network(x)
I got the following error:
ValueError: Cannot feed value of shape (3375, 50, 50, 2) for Tensor 'Reshape:0', which has shape '(?, 5000)'
Does anyone know what is the issue with my code, and how can I fix it?
The data value is (3375, 50, 50, 2)
Thank you for anyone's input!
I think that the problem is that you use the same variable name x for the placeholder and the reshape, in lines
x = tf.placeholder('float', shape = [None,50,50,2])
and
x = tf.reshape(x, [-1, dim])
so that when you
feed_dict={x: your_val}
you are feeding the output of the reshape operation.
You should have different names, for instance
x_placeholder = tf.placeholder('float', shape = [None,50,50,2])
x_reshaped = tf.reshape(x, [-1, dim])
and then
feed_dict={x_placeholder: your_val}