TensorFlow2.0使用DenseFeature作为Functional API第一层时所遇到的问题

丶灬走出姿态 提交于 2020-02-17 20:12:53

本人小白,这几天在学习TensorFlow2.0,想使用Keras的Functional API来搭建一个简单的网络。

起初是按照网上的“心脏病预测实例”一步一步的操作,ok,没什么大问题。但我打算把model的表达方式用Functional API的方式来实现时却报错AttributeError: ‘DenseFeatures’ object has no attribute ‘shape’。

下面是我的代码,其余部分就不贴出来了,和例子一样。

# 以下为我自己改写的代码
feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
dense1 = tf.keras.layers.Dense(128, activation='relu')(feature_layer)
dense2 = tf.keras.layers.Dense(128, activation='relu')(dense1)
dense3 = tf.keras.layers.Dense(1, activation='sigmoid')(dense2)
model = tf.keras.Model(inputs=feature_layer, outputs=dense3)

# 以下为例子中的代码
# model = tf.keras.Sequential([
#     tf.keras.layers.DenseFeatures(feature_columns),
#     tf.keras.layers.Dense(128, activation='relu'),
#     tf.keras.layers.Dense(128, activation='relu'),
#     tf.keras.layers.Dense(1, activation='sigmoid')
# ])

当时卡了好久,最终在GitHub上找到了解决办法,在这里分享给大家,希望大家不要再遇到这个坑了。

解决方法:As unfortunate as this is, for now you will have to make it work by assigning tf.keras.Input to each original feature column that you have, i.e., those numeric feature column and categorical feature columns.

好吧,试着改了一下我的代码,果然能跑通了,呜呜呜呜呜呜呜卡了半个多月的问题终于解决了!!!

下面是使用Functional API + DenseFeature实现代码:

from __future__ import absolute_import, division, print_function

import numpy as np
import pandas as pd

# !pip install tensorflow==2.0.0-alpha0
import tensorflow as tf

from tensorflow import feature_column
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split

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

train, test = train_test_split(dataframe, test_size=0.2)
train, val = train_test_split(train, test_size=0.2)
print(len(train), 'train examples')
print(len(val), 'validation examples')
print(len(test), 'test examples')


# A utility method to create a tf.data dataset from a Pandas Dataframe
def df_to_dataset(dataframe, shuffle=True, batch_size=32):
    dataframe = dataframe.copy()
    labels = dataframe.pop('target')
    ds = tf.data.Dataset.from_tensor_slices((dict(dataframe), labels))
    if shuffle:
        ds = ds.shuffle(buffer_size=len(dataframe))
    ds = ds.batch(batch_size)
    return ds


batch_size = 5  # A small batch sized is used for demonstration purposes
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

age = feature_column.numeric_column("age")

feature_columns = []
feature_layer_inputs = {}

# numeric cols
for header in ['age', 'trestbps', 'chol', 'thalach', 'oldpeak', 'slope', 'ca']:
    feature_columns.append(feature_column.numeric_column(header))
    feature_layer_inputs[header] = tf.keras.Input(shape=(1,), name=header)

# bucketized cols
age_buckets = feature_column.bucketized_column(age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65])
feature_columns.append(age_buckets)

# indicator cols
thal = feature_column.categorical_column_with_vocabulary_list(
    'thal', ['fixed', 'normal', 'reversible'])
thal_one_hot = feature_column.indicator_column(thal)
feature_columns.append(thal_one_hot)
feature_layer_inputs['thal'] = tf.keras.Input(shape=(1,), name='thal', dtype=tf.string)

# embedding cols
thal_embedding = feature_column.embedding_column(thal, dimension=8)
feature_columns.append(thal_embedding)

# crossed cols
crossed_feature = feature_column.crossed_column([age_buckets, thal], hash_bucket_size=1000)
crossed_feature = feature_column.indicator_column(crossed_feature)
feature_columns.append(crossed_feature)

batch_size = 32
train_ds = df_to_dataset(train, batch_size=batch_size)
val_ds = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds = df_to_dataset(test, shuffle=False, batch_size=batch_size)

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)
feature_layer_outputs = feature_layer(feature_layer_inputs)

x = layers.Dense(128, activation='relu')(feature_layer_outputs)
x = layers.Dense(64, activation='relu')(x)

baggage_pred = layers.Dense(1, activation='sigmoid')(x)

model = keras.Model(inputs=[v for v in feature_layer_inputs.values()], outputs=baggage_pred)

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.fit(train_ds,
          validation_data=val_ds,
          epochs=5)

test_loss, test_acc = model.evaluate(test_ds)
print('===================\nTest accuracy:', test_acc)

网址:Unable to use FeatureColumn with Keras Functional API

能访问上面网址的小伙伴可以看看他们的详细讨论。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!