Tensorflow error: Invalid argument: shape must be a vector

Clement

I was trying out tensorflow with the Titanic data from Kaggle:https://www.kaggle.com/c/titanic

Here's the code I tried to implement from Sendex:https://www.youtube.com/watch?v=PwAGxqrXSCs&index=46&list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v#t=398.046664

import tensorflow as tf
import cleanData
import numpy as np

train, test = cleanData.read_and_clean()
train = train[['Pclass', 'Sex', 'Age', 'Fare', 'Child', 'Fam_size', 'Title', 'Mother', 'Survived']]

# one hot
train['Died'] = int('0')
train["Died"][train["Survived"] == 0] = 1

print(train.head())

n_nodes_hl1 = 500
n_classes = 2
batch_size = 100

# tf graph input
x = tf.placeholder("float", [None, 8])
y = tf.placeholder("float")

def neural_network_model(data):

    hidden_layer_1 = {'weights':tf.Variable(tf.random_normal([8, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal(n_nodes_hl1))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}

    l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases'])
    l1 = tf.nn.relu(l1)

    output = tf.matmul(l1, 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(prediction,y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    desired_epochs = 10

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(desired_epochs):
            epoch_loss = 0
            for _ in range(int(train.shape[0])/batch_size):
                x_epoch, y_epoch = train.next_batch(batch_size)
                _, c = sess.run([optimizer, cost], feed_dict= {x:x, y:y})
                epoch_loss += c
            print('Epoch', epoch, 'completed out of', desired_epochs, 'loss:', epoch_loss)

        correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y,1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        print('Training accuracy:', accuracy.eval({x:x, y:y}))



train_neural_network(x)

When I ran the code I got an error that said: "W tensorflow/core/framework/op_kernel.cc:909] Invalid argument: shape must be a vector of {int32,int64}, got shape []"

Is there a way around this? I saw a post on Github for tensorflow's code and apparently the library doesn't take pandas dataframe as an input..

mrry

I think the error is on this line:

    hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([8, n_nodes_hl1])),
                      'biases': tf.Variable(tf.random_normal(n_nodes_hl1))}

The shape argument to tf.random_normal() must be a 1-D vector (or list, or array) of integers. For the 'biases' variable, you're passing a single integer, n_nodes_hl1. The fix is simple, just wrap that argument in a list:

    hidden_layer_1 = {...,
                      'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

cat: write error: Invalid argument

来自分类Dev

Array / Vector as method argument

来自分类Dev

调用 Stackdriver Error Reporting API 时 INVALID_ARGUMENT(400 错误)

来自分类Dev

MacScript Invalid procedure call or argument

来自分类Dev

Tensorflow:用于reshape()的get_shape()

来自分类Dev

如何引发std :: invalid_argument错误?

来自分类Dev

Invalid Binary Error

来自分类Dev

如何在Android中将<vector>放在<shape>中?

来自分类Dev

在TensorFlow文档中无法理解shape(output)=(shape(value)-ksize + 1)/步幅

来自分类Dev

TensorFlow:AttributeError:'Tensor'对象没有属性'shape'

来自分类Dev

List(vector)与tensorflow要求的形状不匹配

来自分类Dev

"Extra argument in call" error in Swift

来自分类Dev

Error: invalid instruction suffix for `push'

来自分类Dev

CUDA invalid device symbol error

来自分类Dev

Invalid target for Validator in spring error?

来自分类Dev

WTSEnumerateServers ERROR_INVALID_DOMAINNAME

来自分类Dev

Google Cloud Vision API上的INVALID_ARGUMENT请求失败

来自分类Dev

Firebase onCall云功能请求返回“ INVALID_ARGUMENT”

来自分类Dev

Microsoft C ++异常:内存位置处的std :: invalid_argument

来自分类Dev

Invalid_argument "String.sub / Bytes.sub"

来自分类Dev

Firebase / Google Cloud Function cron 函数返回 INVALID_ARGUMENT

来自分类Dev

Postgis : ERROR: parse error - invalid geometry

来自分类Dev

error: expression must have integral or enum type

来自分类Dev

lmer error: grouping factor must be < number of observations

来自分类Dev

C , Error: Expression must be a modifiable lvalue

来自分类Dev

使用std :: vector时,抽象类类型'Shape'的新表达式无效

来自分类Dev

ActiveRecord::StatementInvalid: PG::Error: ERROR: must be owner of database

来自分类Dev

Swift error: missing argument label 'name:' in call

来自分类Dev

AttributeError:模块“ tensorflow.python.framework.tensor_shape”没有属性“ scalar”

Related 相关文章

  1. 1

    cat: write error: Invalid argument

  2. 2

    Array / Vector as method argument

  3. 3

    调用 Stackdriver Error Reporting API 时 INVALID_ARGUMENT(400 错误)

  4. 4

    MacScript Invalid procedure call or argument

  5. 5

    Tensorflow:用于reshape()的get_shape()

  6. 6

    如何引发std :: invalid_argument错误?

  7. 7

    Invalid Binary Error

  8. 8

    如何在Android中将<vector>放在<shape>中?

  9. 9

    在TensorFlow文档中无法理解shape(output)=(shape(value)-ksize + 1)/步幅

  10. 10

    TensorFlow:AttributeError:'Tensor'对象没有属性'shape'

  11. 11

    List(vector)与tensorflow要求的形状不匹配

  12. 12

    "Extra argument in call" error in Swift

  13. 13

    Error: invalid instruction suffix for `push'

  14. 14

    CUDA invalid device symbol error

  15. 15

    Invalid target for Validator in spring error?

  16. 16

    WTSEnumerateServers ERROR_INVALID_DOMAINNAME

  17. 17

    Google Cloud Vision API上的INVALID_ARGUMENT请求失败

  18. 18

    Firebase onCall云功能请求返回“ INVALID_ARGUMENT”

  19. 19

    Microsoft C ++异常:内存位置处的std :: invalid_argument

  20. 20

    Invalid_argument "String.sub / Bytes.sub"

  21. 21

    Firebase / Google Cloud Function cron 函数返回 INVALID_ARGUMENT

  22. 22

    Postgis : ERROR: parse error - invalid geometry

  23. 23

    error: expression must have integral or enum type

  24. 24

    lmer error: grouping factor must be < number of observations

  25. 25

    C , Error: Expression must be a modifiable lvalue

  26. 26

    使用std :: vector时,抽象类类型'Shape'的新表达式无效

  27. 27

    ActiveRecord::StatementInvalid: PG::Error: ERROR: must be owner of database

  28. 28

    Swift error: missing argument label 'name:' in call

  29. 29

    AttributeError:模块“ tensorflow.python.framework.tensor_shape”没有属性“ scalar”

热门标签

归档