关于卷轴流Bool或Float的问题?

时间:2021-12-15 02:58:18

I am struggling with Tensorflow to create my neural network on Python 3.6. When I launch the code I have this following issue... My Database is 1100000 x 8 with one dependent variable (boolean 1 or 0) and 6 independent variables (float). I already search for response on Stack but didn't find a way to fix it. Thanks

我正在为在Python 3.6上创建神经网络而奋斗。当我启动代码时,我有以下问题……我的数据库是1100000 x 8,有一个因变量(布尔值1或0)和6个自变量(浮点数)。我已经在堆栈上搜索响应,但是没有找到修复的方法。谢谢

ISSUE

问题

Traceback (most recent call last):

  File "<ipython-input-257-6982dfbaacea>", line 1, in <module>
    runfile('/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/DeepGENERALI.py', wdir='/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework')

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/DeepGENERALI.py", line 102, in <module>
    y = multilayer_perceptron(x, weights, biases)

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/DeepGENERALI.py", line 63, in multilayer_perceptron
    layer_1 = tf.add(tf.matmul(x,weights['h1'], biases['b1']))

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1801, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1263, in _mat_mul
    transpose_b=transpose_b, name=name)

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 697, in apply_op
    attr_value.b = _MakeBool(value, key)

  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 169, in _MakeBool
    (arg_name, repr(v)))

TypeError: Expected bool for argument 'transpose_a' not <tf.Variable 'Variable_724:0' shape=(500,) dtype=float32_ref>.

CODE

代码

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split

#Read the data set
def read_dataset():
    df = pd.read_csv("/Users/Samy/Desktop/Work/Dauphine/Doctorat/Methodology/Base/DeepGENERALI.csv",sep=",",keep_default_na=False)
    x = df [df.columns[1:7]].values
    y = df [df.columns[0]] #.values

#Encode the dependent variable
    encoder = LabelEncoder()
    encoder.fit(y)
    y = encoder.transform(y)
    y = one_hot_encode(y)
    print(x.shape)
    return (x, y)

#Define the encoder function
def one_hot_encode(labels):
    n_labels = len(labels)
    n_unique_labels = len(np.unique(labels))
    one_hot_encode = np.zeros((n_labels, n_unique_labels))
    one_hot_encode[np.arange(n_labels), labels] = 1
    return one_hot_encode

# Read the dataset
x, y = read_dataset()

train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.20, random_state=415)

print(train_x.shape)
print(train_y.shape)
print(test_x.shape)

#Parameters
learning_rate = 0.01
training_epochs = 1000
cost_history = np.empty(x.shape[1], dtype=float)
n_dim = x.shape[1]
print("n_dim", n_dim)
n_class = 10
model_path = "/Users/Samy/Desktop/Work/Dauphine/Doctorat/Methodology/Base/"
#10


n_hidden_1 = 500
n_hidden_2 = 500
n_hidden_3 = 500
n_hidden_4 = 500

x = tf.placeholder(tf.float32, [None, n_dim])
W = tf.Variable(tf.zeros([n_dim, n_class]))
b = tf.Variable(tf.zeros([n_class]))
y_ = tf.placeholder(tf.float32, [None, n_class])

#Define the model
def multilayer_perceptron(x, weights, biases):
    #Hidden layer with RELU activationsd
    layer_1 = tf.add(tf.matmul(x,weights['h1'], biases['b1']))
    layer_1 = tf.nn.sigmoid(layer_1)
    
    layer_2 = tf.add(tf.matmul(layer_1,weights['h2'], biases['b2']))
    layer_2 = tf.nn.sigmoid(layer_2)
    
    layer_3 = tf.add(tf.matmul(layer_2,weights['h3'], biases['b3']))
    layer_3 = tf.nn.sigmoid(layer_2)
    
    layer_4 = tf.add(tf.matmul(layer_3,weights['h4'], biases['b4']))
    layer_4 = tf.nn.relu(layer_2)

    out_layer = tf.matmul(layer_4, weights['out'], biases['out'])
    return out_layer



weights = {
        'h1' : tf.Variable(tf.truncated_normal([n_dim, n_hidden_1])),
        'h2' : tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2])),
        'h3' : tf.Variable(tf.truncated_normal([n_hidden_2, n_hidden_3])),
        'h4' : tf.Variable(tf.truncated_normal([n_hidden_3, n_class])),
        'out' : tf.Variable(tf.truncated_normal([n_hidden_4, n_class])),
        }


biases = {
        'b1' : tf.Variable(tf.truncated_normal([n_hidden_1])),
        'b2' : tf.Variable(tf.truncated_normal([n_hidden_2])),
        'b3' : tf.Variable(tf.truncated_normal([n_hidden_3])),
        'b4' : tf.Variable(tf.truncated_normal([n_hidden_4])),
        'out' : tf.Variable(tf.truncated_normal([n_class])),
        }

#initialize all the variables

init = tf.global_variables_initializer()
saver = tf.train.Saver()

y = multilayer_perceptron(x, weights, biases)
cost_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = y, labels=y_))
training_step = tf.train.GradientDecentOptimizet(learning_rate).minimize(cost_function)

sess = tf.Session()
sess.run(init)

mse_history = []
accuracy_history = []



# training
for epoch in range(training_epochs):
    sess.run(training_step, feed_dict={x: train_x, y_: train_y})
    cost = sess.run(cost_function, feed_dict={x: train_x, y_:train_y})
    cost_history = np.append(cost_history, cost)
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    
    pred_y = sess.run(y, feed_dict={x: test_x})
    mse = tf.reduce_mean(tf.square(pred_y - test_y))
    mse_ =sess.un(mse)
    mse_history.append(mse_)
    accuracy = (sess.run(accuracy, feed_dict={x: train_x, y_: train_y}))
    accuracy_history.append(accuracy)
    
print('epoch:', epoch, '-', 'cost', cost, "-MSE:", mse_, "-Train Accuracy:", accuracy)
    
plt.plot(mse_history, 'r')
plt.show()
plt.plot(accuracy_history)
plt.show

pred_y = sess.run(y, feed_dict={x: test_x})
mse = tf.reduce_mean(tf.square(pred_y - test_y))
print("MSE: %.4f" % sess.run(mse))

1 个解决方案

#1


0  

It seems you have neglected to close your parentheses in the definition of multilayer_perceptron().

似乎您忽略了在multilayer_perceptron()的定义中关闭括号。

Each layer should be: layer = tf.add(tf.matmul(x, weights[h]), biases[h). At this point you can add your activation function (sigmoid, tanh, relu).

每个层应该是:layer = tf.add(tf)。matmul(x,权重[h]),偏见(h)。此时,您可以添加您的激活函数(sigmoid、tanh、relu)。

You're also completely missing tf.add() on your output layer, and are trying to run a matrix multiplication with three inputs, one of which are your biases.

在输出层上,你也完全忽略了tf.add(),并尝试用三个输入来运行矩阵乘法,其中一个是你的偏差。

Fixed

固定

#Define the model
def multilayer_perceptron(x, weights, biases):
    #Hidden layer with RELU activationsd
    layer_1 = tf.add(tf.matmul(x,weights['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)

    layer_2 = tf.add(tf.matmul(layer_1,weights['h2']), biases['b2'])
    layer_2 = tf.nn.sigmoid(layer_2)

    layer_3 = tf.add(tf.matmul(layer_2,weights['h3']), biases['b3'])
    layer_3 = tf.nn.sigmoid(layer_2)

    layer_4 = tf.add(tf.matmul(layer_3,weights['h4']), biases['b4'])
    layer_4 = tf.nn.relu(layer_2)

    out_layer = tf.add(tf.matmul(layer_4, weights['out']), biases['out'])
    return out_layer

#1


0  

It seems you have neglected to close your parentheses in the definition of multilayer_perceptron().

似乎您忽略了在multilayer_perceptron()的定义中关闭括号。

Each layer should be: layer = tf.add(tf.matmul(x, weights[h]), biases[h). At this point you can add your activation function (sigmoid, tanh, relu).

每个层应该是:layer = tf.add(tf)。matmul(x,权重[h]),偏见(h)。此时,您可以添加您的激活函数(sigmoid、tanh、relu)。

You're also completely missing tf.add() on your output layer, and are trying to run a matrix multiplication with three inputs, one of which are your biases.

在输出层上,你也完全忽略了tf.add(),并尝试用三个输入来运行矩阵乘法,其中一个是你的偏差。

Fixed

固定

#Define the model
def multilayer_perceptron(x, weights, biases):
    #Hidden layer with RELU activationsd
    layer_1 = tf.add(tf.matmul(x,weights['h1']), biases['b1'])
    layer_1 = tf.nn.sigmoid(layer_1)

    layer_2 = tf.add(tf.matmul(layer_1,weights['h2']), biases['b2'])
    layer_2 = tf.nn.sigmoid(layer_2)

    layer_3 = tf.add(tf.matmul(layer_2,weights['h3']), biases['b3'])
    layer_3 = tf.nn.sigmoid(layer_2)

    layer_4 = tf.add(tf.matmul(layer_3,weights['h4']), biases['b4'])
    layer_4 = tf.nn.relu(layer_2)

    out_layer = tf.add(tf.matmul(layer_4, weights['out']), biases['out'])
    return out_layer