tensorflow api 2.0 张量对象仅在启用急切执行时才可迭代。要迭代此张量,请使用 tf.map_fn

tensorflow api 2.0 tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn

我正在尝试使用 tensorflow api 2.

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame({'A': np.array([100, 105.4, 108.3, 111.1, 113, 114.7]),
                   'B': np.array([11, 11.8, 12.3, 12.8, 13.1,13.6]),
                   'C': np.array([55, 56.3, 57, 58, 59.5, 60.4]),
                   'Target': np.array([4000, 4200.34, 4700, 5300, 5800, 6400])})

featcols = [
    tf.feature_column.numeric_column("A"),
    tf.feature_column.numeric_column("B"),
    tf.feature_column.numeric_column("C")
]

model = tf.estimator.LinearRegressor(featcols)

features = tf.convert_to_tensor(["A", "B", "C"])

def train_input_fn():
    training_dataset = (
        tf.data.Dataset.from_tensor_slices(
            (
                tf.cast(df[[features]].values, tf.float32),
                tf.cast(df['Target'].values, tf.float32)
            )
        )
    )
    return training_dataset

model.train(train_input_fn)

最后一行令我震惊:

TypeError: Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn

另外,它给了我一个警告:

Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.

这完成没有错误。但是我没有测试过。刚刚安装了 tensorflow 2.0 alpha。

请查看 docs 以获取更多帮助。

import tensorflow as tf
import pandas as pd
import numpy as np



df = pd.DataFrame(data={'A': np.array([100, 105.4, 108.3, 111.1, 113, 114.7]),
                        'B': np.array([11, 11.8, 12.3, 12.8, 13.1,13.6]),
                        'C': np.array([55, 56.3, 57, 58, 59.5, 60.4]),
                        'Target': np.array([4000, 4200.34, 4700, 5300, 5800, 6400])})
print (df.describe())
featcols = [
    tf.feature_column.numeric_column("A"),
    tf.feature_column.numeric_column("B"),
    tf.feature_column.numeric_column("C")
]

model = tf.estimator.LinearRegressor(featcols)


def make_input_fn():
  def train_input_fn():
    label = df.pop('Target')
    print( label )
    print ( df )
    ds = tf.data.Dataset.from_tensor_slices((dict(df), label))
    ds = ds.batch(1).repeat(1)
    return ds

  return train_input_fn


model.train(make_input_fn())

我也在这里展示了为我打印的内容。

Limited tf.compat.v2.summary API due to missing TensorBoard installation
Limited tf.summary API due to missing TensorBoard installation
                A          B         C       Target
count    6.000000   6.000000   6.00000     6.000000
mean   108.750000  12.433333  57.70000  5066.723333
std      5.421716   0.939503   2.01792   937.309351
min    100.000000  11.000000  55.00000  4000.000000
25%    106.125000  11.925000  56.47500  4325.255000
50%    109.700000  12.550000  57.50000  5000.000000
75%    112.525000  13.025000  59.12500  5675.000000
max    114.700000  13.600000  60.40000  6400.000000
WARNING: Logging before flag parsing goes to stderr.
W0313 19:30:06.720984 10576 estimator.py:1799] Using temporary folder as model directory: C:\Users6458\AppData\Local\Temp\tmpkk4q3ute
W0313 19:30:06.767783 10576 deprecation.py:323] From C:\tensorflow2\lib\site-packages\tensorflow\python\training\training_util.py:238: Variable.
initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
0    4000.00
1    4200.34
2    4700.00
3    5300.00
4    5800.00
5    6400.00
Name: Target, dtype: float64
       A     B     C
0  100.0  11.0  55.0
1  105.4  11.8  56.3
2  108.3  12.3  57.0
3  111.1  12.8  58.0
4  113.0  13.1  59.5
5  114.7  13.6  60.4
W0313 19:30:06.861381 10576 deprecation.py:323] From C:\tensorflow2\lib\site-packages\tensorflow\python\feature_column\feature_column_v2.py:2758
: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.cast` instead.
W0313 19:30:07.220174 10576 deprecation.py:506] From C:\tensorflow2\lib\site-packages\tensorflow\python\training\slot_creator.py:187: calling Ze
ros.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
2019-03-13 19:30:07.672566: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was
not compiled to use: AVX2