gog*_*sca 7 python keras tensorflow
TF 1.12:
尝试使用 tf.keras.layers 将 Pre-canned estimator 转换为 Keras:
estimator = tf.estimator.DNNClassifier(
model_dir='/tmp/keras',
feature_columns=deep_columns,
hidden_units = [100, 75, 50, 25],
config=run_config)
Run Code Online (Sandbox Code Playgroud)
到使用 tf.keras.layers 的 Keras 模型:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu, input_shape=(14,)))
model.add(tf.keras.layers.Dense(75))
model.add(tf.keras.layers.Dense(50))
model.add(tf.keras.layers.Dense(25))
model.add(tf.keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer=tf.keras.optimizers.RMSprop(), loss=tf.keras.losses.binary_crossentropy, metrics=['accuracy'])
model.summary()
estimator = tf.keras.estimator.model_to_estimator(model, model_dir='/tmp/keras', config=run_config)
Run Code Online (Sandbox Code Playgroud)
当我运行 Keras 模型时,我得到:
for n in range(40 // 2):
estimator.train(input_fn=train_input_fn)
results = estimator.evaluate(input_fn=eval_input_fn)
# Display evaluation metrics
tf.logging.info('Results at epoch %d / %d', (n + 1) * 2, 40)
tf.logging.info('-' * 60)
Run Code Online (Sandbox Code Playgroud)
当我训练它时,我收到此错误:
主要代码:https : //github.com/tensorflow/models/blob/master/official/wide_deep/census_main.py
KeyError:“传递给特征的字典没有在 keras 模型中定义的预期输入键。\n\t预期键:{'dense_50_input'}\n\tfeatures 键:{'workclass', 'occupation', 'hours_per_week' , 'marital_status', 'relationship', 'race', 'fnlwgt', 'education', 'gender', 'capital_loss', 'capital_gain', 'age', 'education_num', 'native_country'}\n\t区别: {'workclass', 'occupation', 'hours_per_week', 'marital_status', 'relationship', 'dense_50_input', 'race', 'fnlwgt', 'education', 'gender', 'capital_loss', 'capital_gain', '年龄', 'education_num', 'native_country'}"
这是我的 input_fn:
def input_fn(data_file, num_epochs, shuffle, batch_size):
"""Generate an input function for the Estimator."""
assert tf.gfile.Exists(data_file), (
'%s not found. Please make sure you have run census_dataset.py and '
'set the --data_dir argument to the correct path.' % data_file)
def parse_csv(value):
tf.logging.info('Parsing {}'.format(data_file))
columns = tf.decode_csv(value, record_defaults=_CSV_COLUMN_DEFAULTS)
features = dict(zip(_CSV_COLUMNS, columns))
labels = features.pop('income_bracket')
classes = tf.equal(labels, '>50K') # binary classification
return features, classes
# Extract lines from input files using the Dataset API.
dataset = tf.data.TextLineDataset(data_file)
if shuffle:
dataset = dataset.shuffle(buffer_size=_NUM_EXAMPLES['train'])
dataset = dataset.map(parse_csv, num_parallel_calls=5)
# We call repeat after shuffling, rather than before, to prevent separate
# epochs from blending together.
dataset = dataset.repeat(num_epochs)
dataset = dataset.batch(batch_size)
return dataset
def train_input_fn():
return input_fn(train_file, 2, True, 40)
def eval_input_fn():
return input_fn(test_file, 1, False, 40)
Run Code Online (Sandbox Code Playgroud)
小智 -1
您需要添加一个输入层:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=your_tensor_shape, name=your_feature_key))
model.add(tf.keras.layers.Dense(100, activation=tf.nn.relu))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1890 次 |
最近记录: |