2017-08-24 50 views
0

我正在使用Tensorflow的RBF網絡上工作,但是有這樣的錯誤出現在第112行,它表示:ValueError:無法爲Tensor'佔位符:0'提供形狀值(40,13),它具有形狀'(?,12)'Tensorflow中我的RBF網絡問題?

下面是我的代碼如下。我通過遵循this tutorial爲我的RBF網絡創建了自己的激活功能。另外,如果您還有其他問題需要解決,請向我指出,因爲我對Tensorflow非常陌生,因此獲得我可以獲得的任何反饋信息都會有幫助。

import tensorflow as tf 
import numpy as np 
import math 
from sklearn import datasets 
from sklearn.model_selection import train_test_split 
from tensorflow.python.framework import ops 
ops.reset_default_graph() 

RANDOM_SEED = 42 
tf.set_random_seed(RANDOM_SEED) 

boston = datasets.load_boston() 

data = boston["data"] 
target = boston["target"] 

N_INSTANCES = data.shape[0] 
N_INPUT = data.shape[1] - 1 
N_CLASSES = 3 
TEST_SIZE = 0.1 
TRAIN_SIZE = int(N_INSTANCES * (1 - TEST_SIZE)) 
batch_size = 40 
training_epochs = 400 
learning_rate = 0.001 
display_step = 20 
hidden_size = 200 

target_ = np.zeros((N_INSTANCES, N_CLASSES)) 

data_train, data_test, target_train, target_test = train_test_split(data, target_, test_size=0.1, random_state=100) 

x_data = tf.placeholder(shape=[None, N_INPUT], dtype=tf.float32) 
y_target = tf.placeholder(shape=[None, N_CLASSES], dtype=tf.float32) 

# creates activation function 
def gaussian_function(input_layer): 
    initial = math.exp(-2*math.pow(input_layer, 2)) 
    return initial 

np_gaussian_function = np.vectorize(gaussian_function) 

def d_gaussian_function(input_layer): 
    initial = -4 * input_layer * math.exp(-2*math.pow(input_layer, 2)) 
    return initial 

np_d_gaussian_function = np.vectorize(d_gaussian_function) 

np_d_gaussian_function_32 = lambda input_layer: np_d_gaussian_function(input_layer).astype(np.float32) 

def tf_d_gaussian_function(input_layer, name=None): 
    with ops.name_scope(name, "d_gaussian_function", [input_layer]) as name: 
     y = tf.py_func(np_d_gaussian_function_32, [input_layer],[tf.float32], name=name, stateful=False) 
    return y[0] 

def py_func(func, inp, Tout, stateful=True, name=None, grad=None): 
    rnd_name = 'PyFunGrad' + str(np.random.randint(0, 1E+8)) 

    tf.RegisterGradient(rnd_name)(grad) 
    g = tf.get_default_graph() 
    with g.gradient_override_map({"PyFunc": rnd_name}): 
     return tf.py_func(func, inp, Tout, stateful=stateful, name=name) 

def gaussian_function_grad(op, grad): 
    input_variable = op.inputs[0] 
    n_gr = tf_d_gaussian_function(input_variable) 
    return grad * n_gr 

np_gaussian_function_32 = lambda input_layer: np_gaussian_function(input_layer).astype(np.float32) 

def tf_gaussian_function(input_layer, name=None): 
    with ops.name_scope(name, "gaussian_function", [input_layer]) as name: 
     y = py_func(np_gaussian_function_32, [input_layer], [tf.float32], name=name, grad=gaussian_function_grad) 
    return y[0] 
# end of defining activation function 

def rbf_network(input_layer, weights): 
    layer1 = tf.matmul(tf_gaussian_function(input_layer), weights['h1']) 
    layer2 = tf.matmul(tf_gaussian_function(layer1), weights['h2']) 
    output = tf.matmul(tf_gaussian_function(layer2), weights['output']) 
    return output 

weights = { 
    'h1': tf.Variable(tf.random_normal([N_INPUT, hidden_size], stddev=0.1)), 
    'h2': tf.Variable(tf.random_normal([hidden_size, hidden_size], stddev=0.1)), 
    'output': tf.Variable(tf.random_normal([hidden_size, N_CLASSES], stddev=0.1)) 
} 

pred = rbf_network(x_data, weights) 

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y_target)) 
my_opt = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y_target, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

init = tf.global_variables_initializer() 
sess = tf.InteractiveSession() 
sess.run(init) 

# Training loop 
for epoch in range(training_epochs): 
    avg_cost = 0. 
    total_batch = int(data_train.shape[0]/batch_size) 
    for i in range(total_batch): 
     randidx = np.random.randint(int(TRAIN_SIZE), size=batch_size) 
     batch_xs = data_train[randidx, :] 
     batch_ys = target_train[randidx, :] 

     sess.run(my_opt, feed_dict={x_data: batch_xs, y_target: batch_ys}) 
     avg_cost += sess.run(cost, feed_dict={x_data: batch_xs, y_target: batch_ys})/total_batch 

     if epoch % display_step == 0: 
      print("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost)) 
      train_accuracy = sess.run(accuracy, feed_dict={x_data: batch_xs, y_target: batch_ys}) 
      print("Training accuracy: %.3f" % train_accuracy) 

test_acc = sess.run(accuracy, feed_dict={x_data: data_test, y_target: target_test}) 
print("Test accuracy: %.3f" % (test_acc)) 

sess.close() 

回答

1

我也是新來的Tensorflow,這是我的第一個答案在stackoverflow。我試過你的代碼,我得到了同樣的錯誤。

你可以在錯誤代碼ValueError: Cannot feed value of shape (40, 13) for Tensor 'Placeholder:0', which has shape '(?, 12)看到,有在第一個佔位符的形狀不匹配:

x_data = tf.placeholder(shape=[None, N_INPUT], dtype=tf.float32) 

所以我不知道爲什麼N_INPUT在這條線-1

N_INPUT = data.shape[1] - 1 

我試着刪除它,代碼運行。雖然看起來網絡沒有學習。

0

正如前面所說,你應該有N_Input = data.shape[1]

實際上data.shape[0]與你在數據集中的實現數有關,data.shape[1]告訴我們網絡應該考慮多少特性。

根據定義輸入圖層的大小,無論您將通過feed_dict建議的網絡數量爲多少。

加上波士頓的數據集是一個迴歸問題而softmax_cross_entropy是分類問題的成本函數。您可以嘗試tf.square來評估你想要什麼,你的預測,什麼之間的歐氏距離:

cost = tf.reduce_mean(tf.square(pred - y_target)) 

您將看到您的網絡是學習,雖然精度不是很高。

編輯:

您的代碼實際上是學習很好,但你用錯了工具來衡量它。

主要是,您的錯誤仍然存​​在於您正在處理迴歸問題而不是分類問題的事實中。

在分類的問題,您可以評估的準確性您持續的使用學習過程

correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y_target, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

它由,檢查是否預測類相同的預期類中x_test輸入。

在迴歸問題中,這樣做是毫無意義的,因爲您正在尋找一個實數數字,即從分類的角度來看無限可能性。

在迴歸問題中,您可以估計預測值和預期值之間的錯誤(平均值或其他值)。我們可以用我的建議如下:

cost = tf.reduce_mean(tf.square(pred - y_target)) 

我修改您的代碼因此這裏是

pred = rbf_network(x_data, weights) 

cost = tf.reduce_mean(tf.square(pred - y_target)) 
my_opt = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 

#correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y_target, 1)) 
#accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 

init = tf.global_variables_initializer() 
sess = tf.InteractiveSession() 
sess.run(init) 

plt.figure("Error evolution") 
plt.xlabel("N_epoch") 
plt.ylabel("Error evolution") 
tol = 5e-4 
epoch, err=0, 1 
# Training loop 
while epoch <= training_epochs and err >= tol: 
    avg_cost = 0. 
    total_batch = int(data_train.shape[0]/batch_size) 
    for i in range(total_batch): 
     randidx = np.random.randint(int(TRAIN_SIZE), size=batch_size) 
     batch_xs = data_train[randidx, :] 
     batch_ys = target_train[randidx, :] 

     sess.run(my_opt, feed_dict={x_data: batch_xs, y_target: batch_ys}) 
     avg_cost += sess.run(cost, feed_dict={x_data: batch_xs, y_target: batch_ys})/total_batch 
    plt.plot(epoch, avg_cost, marker='o', linestyle="none", c='k') 
    plt.pause(0.05) 
    err = avg_cost 
    if epoch % 10 == 0: 
     print("Epoch: {}/{} err = {}".format(epoch, training_epochs, avg_cost)) 

    epoch +=1 

print ("End of learning process") 
print ("Final epoch = {}/{} ".format(epoch, training_epochs)) 
print ("Final error = {}".format(err)) 
sess.close() 

輸出是

Epoch: 0/400 err = 0.107879924503 
Epoch: 10/400 err = 0.00520248359747 
Epoch: 20/400 err = 0.000651647908274 
End of learning process 

Final epoch = 26/400 
Final error = 0.000474644409471 

我們繪製在訓練誤差的演變通過不同的時代enter image description here