欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linear regression using TF(2)

程序员文章站 2022-06-11 22:29:17
...

batch,epoch,iteration batch_size, num_epochs

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7, 0.])

input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x_train}, y_train,
                                              batch_size=4,
                                              num_epochs=1000)

eval_input_fn = tf.contrib.learn.io.numpy_input_fn(
    {"x":x_eval}, y_eval, batch_size=4, num_epochs=1000)

estimator.fit(input_fn=input_fn, steps=1000)
train_loss = estimator.evaluate(input_fn=input_fn)
  • 这里看下log
# config 部分
{'_model_dir': None,
 '_save_checkpoints_secs': 600, 
'_num_ps_replicas': 0, 
'_keep_checkpoint_max': 5, 
'_tf_random_seed': None,
 '_task_type': None, 
'_environment': 'local',
 '_is_chief': True, 
'_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x125321510>, 
'_tf_config': gpu_options {per_process_gpu_memory_fraction: 1}
, '_num_worker_replicas': 0,
 '_task_id': 0,
 '_save_summary_steps': 100, 
'_save_checkpoints_steps': None,
 '_evaluation_master': '', '
_keep_checkpoint_every_n_hours': 10000,
 '_master': ''}

#warning 部分
model directory
attempt to expand dims
no longer supported

#checkpoints和loss的一些保存及打印
Create CheckpointSaverHook.
Saving checkpoints 
loss = 3.25, step = 1
global_step/sec: 661.358
Loss for final step: 6.06924e-10.

#evaluation信息和restore parameter
Starting evaluation at 2017-12-05-18:10:37
Restoring parameters from 
Finished evaluation at 2017-12-05-18:10:39
Saving dict for global step 1000: global_step = 1000, loss = 8.37181e-10
train loss: {'loss': 8.3718071e-10, 'global_step': 1000}
eval loss: {'loss': 0.0025272823, 'global_step': 1000}