简体   繁体   English

如何为线性回归和训练模型创建 tf.data.Datasets

[英]How to create a tf.data.Datasets for linear regression and train model

Can I train a linear regression model with tf.data.Datasets?我可以使用 tf.data.Datasets 训练线性回归模型吗? If I run the following code如果我运行以下代码

import tensorflow as tf
import numpy as np

x = np.linspace(1, 10, num=10**2)
y = 54*x + 33

ds = tf.data.Dataset.from_tensor_slices(list(zip(x, y)))

model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(1, input_shape = [1,]),
        tf.keras.layers.Dense(10, activation="sigmoid"),
        tf.keras.layers.Dense(1)
    ])

model.compile(loss="mean_absolute_error", optimizer="adam")
model.fit(ds, epochs=5)

I get the error我得到错误

ValueError: Target data is missing. Your model was compiled with loss=mean_absolute_error, and therefore expects target data to be provided in `fit()`.

It is possible to train like that?可以这样训练吗?

You need to consider:你需要考虑:

  1. Create dataset like from_tensor_slices((x,y))创建dataset ,如from_tensor_slices((x,y))
  2. Define and take dataset with batch, like : ds = ds.batch(32)使用批处理定义和获取dataset ,例如: ds = ds.batch(32)
import tensorflow as tf
import numpy as np

x = np.linspace(1, 10, num=10**2)
y = 54*x + 33

ds = tf.data.Dataset.from_tensor_slices((x,y))
ds = ds.batch(32)
model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(1, input_shape = [1,]),
        tf.keras.layers.Dense(10, activation="sigmoid"),
        tf.keras.layers.Dense(1)
    ])

model.compile(loss="mean_absolute_error", optimizer="adam")
model.fit(ds, epochs=5)

Output:输出:

Epoch 1/5
4/4 [==============================] - 0s 5ms/step - loss: 329.4714
Epoch 2/5
4/4 [==============================] - 0s 8ms/step - loss: 329.4355
Epoch 3/5
4/4 [==============================] - 0s 11ms/step - loss: 329.3994
Epoch 4/5
4/4 [==============================] - 0s 6ms/step - loss: 329.3628
Epoch 5/5
4/4 [==============================] - 0s 9ms/step - loss: 329.3259

Update: How to create a model and train for linear regression?更新:如何创建模型并训练线性回归? You don't need a complex and large network only a Dense(1) with activation='linear' is OK.您不需要一个复杂而大型的网络,只有一个具有activation='linear'Dense(1)就可以了。

import tensorflow as tf
import numpy as np

x = np.random.rand(10000)
y = 54*x + 33

ds = tf.data.Dataset.from_tensor_slices((x,y))
ds = ds.batch(64)
model = tf.keras.models.Sequential([
        tf.keras.layers.Dense(1, input_shape = [1,]),
        tf.keras.layers.Dense(1, activation='linear')
    ])

model.compile(loss="mean_absolute_error", optimizer="adam")
model.fit(ds, epochs=50)

Epoch 1/50
157/157 [==============================] - 1s 2ms/step - loss: 60.0440
Epoch 2/50
157/157 [==============================] - 0s 2ms/step - loss: 59.6723
Epoch 3/50
157/157 [==============================] - 0s 2ms/step - loss: 59.1068
...
Epoch 48/50
157/157 [==============================] - 0s 2ms/step - loss: 0.1588
Epoch 49/50
157/157 [==============================] - 0s 2ms/step - loss: 0.0053
Epoch 50/50
157/157 [==============================] - 0s 3ms/step - loss: 0.0039

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 tf.data.Datasets 构建 tf,estimator.DNNClassifier - Build tf,estimator.DNNClassifier from tf.data.Datasets 从Tensorflow中的多个tf.data.Datasets中随机抽样 - Randomly sample from multiple tf.data.Datasets in Tensorflow 如何使用 tf.data.Dataset.map 对两个 tf.data.Datasets 进行元素总和,两者都无限迭代? - How to do the element-wise sum of two tf.data.Datasets, both iterating indefinitely, with tf.data.Dataset.map? 当新数据到来时如何在pyspark中重新训练保存的线性回归ML模型 - how to re-train Saved linear regression ML model in pyspark when new data is coming 如何为时间序列数据创建线性回归 model? - How do I create a Linear regression model for a time series data? 将一个 tf.data.Datasets 与另一个的所有其他元素合并 - Merging one tf.data.Datasets with every other element of another one 使用tf.data.Datasets冻结Tensorflow图时确定输入节点 - Determining input nodes when freezing Tensorflow graphs using tf.data.Datasets 如何传递单个列因变量来训练线性回归模型? - How to pass the single columnar dependent variable to train the linear regression model? 如何使用 tf.data.Dataset 训练多元回归 output? - How to to train multi regression output with tf.data.Dataset? 如何创建 for 循环来比较线性回归模型的训练和测试分数 - How to create for loop to compare train and test score of linear regression models
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM