Меня попросили произвести вывод CNN для данного набора данных.
Ошибка
ValueError: Cannot feed value of shape (16, 10) for Tensor 'Placeholder_4:0', which has shape '(?, 60, 11, 1)'
Набор данных включен ниже
Att1 Att2 Att3 Att4 Att5 Att6 Att7 Att8 Att9 Att10 Att11
1 0 1 0.9471 C 0 S 0.8561 0.002 0.004 1.8091
1 0 1 0.992 C 0 S 0.8648 0.002 0.003 1.861
1 0 1 1.0722 C 0 S 2.009 0.002 0.003 3.0876
1 0 1 0.994 C 0 S 0.8754 0.002 0.003 1.8744
1 0 1 1.0121 C 0 S 0.9275 0.002 0.003 1.9447
1 0 1 0.9825 C 0 S 0.9579 0.002 0.003 1.9455
1 0 1 0.7372 C 0 S 0.8699 0.002 0.003 1.6122
1 0 1 0.9533 C 0 S 0.8377 0.002 0.004 1.797
1 0 1 0.9813 C 0 S 0.8647 0.002 0.003 1.8511
1 0 1 0.9693 C 0 S 0.8664 0.001 0.003 1.8402
1 0.0005 1 0.9652 C 0 S 0.8625 0.002 0.003 1.8317
1 0 1 1.0564 C 0 S 0.8796 0.002 0.003 1.941
1 0 1 0.9562 C 0 S 0.8503 0.002 0.003 1.8115
1 0 1 0.9581 C 0 S 0.8491 0.002 0.003 1.8113
1 0 1 0.9919 C 0 S 0.9162 0.0015 0.025 1.9121
1 0 1 0.9443 C 0 S 0.8479 0.002 0.003 1.7973
1 0 1 0.7454 C 0 S 0.8722 0.002 0.003 1.6225
1 0 1 0.9979 C 0 S 1.0774 0.002 0.003 2.0803
1 0 1 0.7275 C 0 S 0.9696 0.0014 0.0025 1.7012
1 0 1 1.089 C 0 S 1.9643 0.0015 0.0025 3.0573
2 0.0005 1 0.9699 C 0 S 0 0.002 0.004 1.8268
2 0 1 0.7045 C 0 S 0 0.0015 0.0029 1.5466
2 0 1 0.9584 C 0 S 0 0.002 0.00298 1.8052
22 0 1 0.744 C 0 S 0 0.0015 0.0025 1.598
2 0 1 0.744 C 0 S 0 0.0015 0.0025 1.598
2 0 1 0.7379 C 0 S 0 0.0019 0.003 1.5926
2 0 1 0.756 C 0 S 0 0.0019 0.003 1.6523
2 0 1 0.737 C 0 S 0 0.002 0.0029 1.7028
2 0 1 0.707 C 0 S 0 0.0015 0.0025 4.249
2 0 1 0.7127 C 0 S 0 0.001 0.003 1.5867
2 0 1 0.7177 C 0 S 0 0.002 0.003 1.5921
2 0 1 0.7299 C 0 S 0 0.002 0.003 1.5706
2 0 1 0.7549 C 0 S 0 0.0014 0.003 1.5835
2 0 1 0.6954 C 0 S 0 0.0019 0.003 1.5239
2 0 1 0.7131 C 0 S 0 0.0019 0.003 1.5401
2 0 1 0.712 C 0 S 0 0.0014 0.0025 1.525
2 0 1 0.7074 C 0 S 0 0.002 0.003 1.5304
2 0 1 0.7382 C 0 S 0 0.002 0.0029 1.5704
2 0 1 0.7052 C 0 S 0 0.0019 0.003 1.5693
2 0 1 0.7178 C 0 S 0 0.002 0.003 1.5438
3 0 1 0.9599 C 0 S 1.2939 0.0015 0.003 2.2583
3 0 1 0.957 C 0 S 0 0.0019 0.003 0.9619
3 0 1 1.0089 C 0 S 0.9696 0.0015 0.0025 1.9825
3 0 1 0.7337 C 0 S 0.99 0.002 0.003 1.7287
3 0 1 0.7238 C 0 S 0.9761 0.001 0.003 1.7042
3 0 1 0.9554 C 0 S 0.9685 0.0019 0.003 1.9299
3 0 1 0.7128 C 0 S 0.821 0.002 0.003 1.539
3 0 1 0.7562 C 0 S 1.0262 0.0019 0.003 1.7875
3 0 1 0.7617 C 0 S 0.9947 0.002 0.004 1.7625
3 0 1 0.7543 C 0 S 0.9934 0.002 0.003 1.7527
3 0 1 0.7243 C 0 S 0.9709 0.002 0.003 1.7003
3 0 1 0.7157 C 0 S 1 0.002 0.003 1.7208
3 0 1 0.7075 C 0 S 0.9861 0.0015 0.0025 1.6977
3 0 1 0.7418 C 0 S 0.9477 0.0019 0.003 1.6946
3 0 1 0.701 C 0 S 0.9645 0.0015 0.0025 1.6695
3 0 1 0.7443 C 0 S 1.0016 0.002 0.003 1.751
3 0 1 0.7101 C 0 S 0.9661 0.002 0.003 1.6813
3 0 1 0.706 C 0 S 0.9734 0.002 0.003 1.6845
3 0 1 0.713 C 0 S 0.9791 0.0015 0.0025 1.6962
3 0 1 0.7085 C 0 S 0.9968 0.0015 0.0025 1.7104
Моя работа приведена ниже. Последняя часть, то есть часть tf.Session (), выдает ошибку под названием «не удалось преобразовать строку в число с плавающей точкой:« C »». Любая помощь, почему эта ошибка поможет мне понять намного лучше.
Заранее спасибо.
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
df = pd.read_excel(r"C:/Users/ggmah/Desktop/HMM Data updated.xlsx")
tf.logging.set_verbosity(tf.logging.INFO)
dff = OneHotEncoder(df)
dfg = pd.get_dummies(df)
# df.head()
# dff
# dfg
o =list(df.columns.values)
label_dict = dict()
for i,value in enumerate(o):
label_dict[i] = value
# label_dict
# len(label_dict)
training_iters = 220
learning_rate = 0.002
batch_size = 16
n_input = 59
n_classes = 11
x = tf.placeholder("float", [None, 60,11,1])
y = tf.placeholder("float", [None, n_classes])
def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
# x: input var
# W: weight fn
# b: bias
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],padding='SAME')
weights = {
'wc1': tf.get_variable('W0', shape=(3,3,1,32), initializer=tf.contrib.layers.xavier_initializer()),
'wc2': tf.get_variable('W1', shape=(3,3,32,64), initializer=tf.contrib.layers.xavier_initializer()),
'wc3': tf.get_variable('W2', shape=(3,3,64,128), initializer=tf.contrib.layers.xavier_initializer()),
'wd1': tf.get_variable('W3', shape=(4*4*128,128), initializer=tf.contrib.layers.xavier_initializer()),
'out': tf.get_variable('W6', shape=(128,n_classes), initializer=tf.contrib.layers.xavier_initializer()),
}
biases = {
'bc1': tf.get_variable('B0', shape=(32), initializer=tf.contrib.layers.xavier_initializer()),
'bc2': tf.get_variable('B1', shape=(64), initializer=tf.contrib.layers.xavier_initializer()),
'bc3': tf.get_variable('B2', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'bd1': tf.get_variable('B3', shape=(128), initializer=tf.contrib.layers.xavier_initializer()),
'out': tf.get_variable('B4', shape=(11), initializer=tf.contrib.layers.xavier_initializer()),
}
# from tensorflow.examples.tutorials.mnist import input_data
# data = input_data.read_data_sets('data/fashion',one_hot=True)
X = dfg[['Att1','Att2','Att3','Att4',
'Att5_C',
'Att6',
'Att7_S',
'Att8','Att9','Att10']]
Y = dfg[['Att11']]
train_X, test_X,train_y,test_y = train_test_split(X,Y,train_size=0.88,random_state=5)
def conv_net(x, weights, biases):
# here we call the conv2d function we had defined above and pass the input image x, weights wc1 and bias bc1.
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
# Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 14*14 matrix.
conv1 = maxpool2d(conv1, k=2)
# Convolution Layer
# here we call the conv2d function we had defined above and pass the input image x, weights wc2 and bias bc2.
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
# Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 7*7 matrix.
conv2 = maxpool2d(conv2, k=2)
conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
# Max Pooling (down-sampling), this chooses the max value from a 2*2 matrix window and outputs a 4*4.
conv3 = maxpool2d(conv3, k=2)
# Fully connected layer
# Reshape conv2 output to fit fully connected layer input
fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
# Output, class prediction
# finally we multiply the fully connected layer with the weights and add a bias term.
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
pred = conv_net(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#Here you check whether the index of the maximum value of the predicted image is equal to the actual labelled image. and
# both will be a column vector.
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
#calculate accuracy across all the given images and average them out.
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
train_loss = []
test_loss = []
train_accuracy = []
test_accuracy = []
summary_writer = tf.summary.FileWriter('./Output', sess.graph)
for i in range(training_iters):
for batch in range(len(train_X)//batch_size):
batch_x = train_X[batch*batch_size:min((batch+1)*batch_size,len(train_X))]
batch_y = train_y[batch*batch_size:min((batch+1)*batch_size,len(train_y))]
# Run optimization op (backprop).
# Calculate batch loss and accuracy
opt = sess.run(optimizer, feed_dict={x: batch_x,
y: batch_y})
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y})
print("Iter " + str(i) + ", Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for all 10000 mnist test images
test_acc,valid_loss = sess.run([accuracy,cost], feed_dict={x: test_X,y : test_y})
train_loss.append(loss)
test_loss.append(valid_loss)
train_accuracy.append(acc)
test_accuracy.append(test_acc)
print("Testing Accuracy:","{:.5f}".format(test_acc))
summary_writer.close()
Я должен составить график, показывающий рост кривой обучения для разных скоростей обучения и разных величин ввода.