Почему я не могу напечатать содержимое объекта Tensor в Python? - PullRequest
0 голосов
/ 09 июля 2019

Я пытаюсь загрузить кадр данных pandas в набор данных Tensorflow.Я что-то упустил, например, атрибут для объекта Tensor?Или это может быть проблема импорта?

Я пытался сделать это в соответствии со следующим примером: https://www.tensorflow.org/beta/tutorials/load_data/pandas

Я не получаю ожидаемый результат.

Я пробовал этот более сложный пример ранее, который дал мне похожие результаты: https://www.tensorflow.org/beta/tutorials/keras/feature_columns

Даже при копировании первого упомянутого примера точно в терминале он не ведет себя так, как показано.

Вот код:

URL = 'https://storage.googleapis.com/applied-dl/heart.csv'
df = pd.read_csv(URL)

df['thal'] = pd.Categorical(df['thal'])
df['thal'] = df.thal.cat.codes

target = df.pop('target')

dataset = tf.data.Dataset.from_tensor_slices((df.values, target.values))

for feat, targ in dataset.take(5):
    print('Features: {}, Target: {}'.format(feat, targ))

Ожидаемый вывод:

Features: [ 63.   1.   1. 145. 233.   1.   2. 150.   0.   2.3  3.   0.   2. ], Target: 0
Features: [ 67.   1.   4. 160. 286.   0.   2. 108.   1.   1.5  2.   3.   3. ], Target: 1
Features: [ 67.   1.   4. 120. 229.   0.   2. 129.   1.   2.6  2.   2.   4. ], Target: 0
Features: [ 37.   1.   3. 130. 250.   0.   0. 187.   0.   3.5  3.   0.   3. ], Target: 0
Features: [ 41.   0.   2. 130. 204.   0.   2. 172.   0.   1.4  1.   0.   3. ], Target: 0

Фактический вывод:

Features: Tensor("IteratorGetNext:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_1:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_1:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_2:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_2:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_3:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_3:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_4:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_4:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_5:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_5:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_6:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_6:1", shape=(), dtype=int64)
Features: Tensor("IteratorGetNext_7:0", shape=(13,), dtype=float64), Target: Tensor("IteratorGetNext_7:1", shape=(), dtype=int64)
...

Редактировать: я пытался добавить после импорта:

tf.enable_eager_execution()

Работает сейчас!

1 Ответ

0 голосов
/ 09 июля 2019

Это сработало для меня:

import tensorflow as tf
import pandas as pd
tf.enable_eager_execution()

URL = 'https://storage.googleapis.com/applied-dl/heart.csv'
df = pd.read_csv(URL)

df['thal'] = pd.Categorical(df['thal'])
df['thal'] = df.thal.cat.codes

target = df.pop('target')

dataset = tf.data.Dataset.from_tensor_slices((df.values, target.values))

for feat, targ in dataset.take(5):
    print('Features: {}, Target: {}'.format(feat, targ))

Попробуйте и дайте мне знать.

...