簡體   English   中英

Tensorflow:將numpy ndarray饋入占位符時出現InvalidArgumentError

[英]Tensorflow: InvalidArgumentError when feeding numpy ndarray to placeholder

我想運行一個簡單的神經網絡,其中包含3個輸入“神經元”(具有三個特征:身高,體重,體脂)和2個輸出“神經元”,以通過softmax logistic回歸將新數據分類為男性或女性。 我整理這些數據只是為了練習將數據輸入模型。 就像在tensorflow網站上的MNIST教程中一樣,但是更簡單,更有形(不是784個輸入功能-給定MNIST圖像的所有像素-我只有3個,而不是幾批訓練我們的模型,我只想在同一數據中對其進行多次訓練)。

我知道這可以通過更簡單的算法來完成,例如k-means聚類。 但是我只想學習在數據很小時如何輸入數據。

我得到這個:

InvalidArgumentError:您必須使用dtype float為占位符張量'Placeholder'輸入值lue

我不知道為什么...因為我已將列表值轉換為np.float32(與tf.float32相同),並且在定義函數之前先對其進行了檢查。 但是,我仍然遇到這個煩人的錯誤。 無論我進行什么更改,在加載到第一個palholder中時,總是會遇到此錯誤:X。

這是我的代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import tensorflow as tf
import time

    #TRAINING DATA  


        #3 VIs en columna

                    #est, pes, %gb_fat
persones = np.asarray([[175,70,2],   #H
                       [155,45,15],  #F 
                       [190,85,8],   #H
                       [185,90,7],   #H
                       [169,60,3],   #H
                       [150,40,13],  #F
                       [173,69,12]], dtype = np.float32)  #H

                        # H , D --> one-hot!! 
etiquetes = np.asarray([[1,0],
                        [0,1],
                        [1,0],
                        [1,0],
                        [1,0],
                        [0,1],
                        [1,0]], dtype = np.float32)


#TESTING DATA
persones_evaluacio = np.asarray([[190,89,4], #H
                                [155,52,16], #D
                                [171,55,18]], dtype = np.float32) #D

etiquetes_evaluacio = np.asarray([[1,0],
                                  [0,1],
                                  [0,1]], dtype = np.float32)

#WE TEST THE DATATYPES
print("dades dels nombres:   ",type(persones[0][0]))
print("tipus estructura de dades de la matriu:    ", type(persones))
time.sleep(3)
print("files de la matriu:")
time.sleep(0.5)
for i in range(len(persones)):
    print(persones[i])
time.sleep(0.5)


def classifica_H_D(nombre_VIs, categories_VD):

    #placeholders
    x = tf.placeholder(tf.float32, [None,nombre_VIs])
    y_reals = tf.placeholder(tf.float32, [None,categories_VD])

    #variables
    w = tf.Variable(tf.zeros([nombre_VIs, categories_VD], dtype = tf.float32))
    b = tf.Variable(tf.zeros([categories_VD], dtype = tf.float32))


    #DEFINE MODEL
    y_predits = tf.nn.softmax(tf.matmul(x,w) + b)

    # define LOSS FUNCTION
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_reals, logits=y_predits))

    #define optimizer to get cross_entropy minimized
    train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy)


    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print("finsaqui")
        time.sleep(2)
        for i in range(1000): #iterate over same data.
            sess.run(train_step, feed_dict = {x : persones, y_reals : etiquetes})
            if i%50:
                print(w.eval(), sess.run(cross_entropy))

        prediccio_correcta = tf.equal(tf.argmax(y_predit,1), tf.argmax(y_correctes,1))
        accuracy = tf.reduce_mean(tf.cast(prediccio_correcta, tf.float32))
        return "\naccuracy: {:.2f}".format(sess.run(accuracy, feed_dict={x: persones_evaluacio, y_reals: etiquetes_evaluacio}))


print(classifica_H_D(3,2))

問題在這一行:

 print(w.eval(), sess.run(cross_entropy)), 

您需要以以下形式為其輸入輸入:

 print(w.eval(), sess.run(cross_entropy, feed_dict = {x : persones, y_reals : etiquetes}))

或更好的方法是:

 for i in range(1000): #iterate over same data.
     _, c_entropy = sess.run([train_step,cross_entropy], feed_dict = {x : persones, y_reals : etiquetes})
     if i%50:
        print(w.eval(), c_entropy)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM