使用tensorflow2写一个简单的线性回归模型的训练与预测

设置的模型

Y = 2 × X − 1 Y=2\times X-1 Y=2×X1

代码

环境

  • tensorflw2.7
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import (Dense,)
import numpy as np


if __name__ == '__main__':
    # x = tf.constant(1.0)
    # print(x.device)

    model = Sequential(
        [
            Dense(units=1,input_shape=[1]),

        ]
    )
    model.compile(loss='mean_squared_error',optimizer='sgd')

    # y = 2x - 1
    xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
    ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)

    model.fit(xs,ys,epochs=500)

    print(model.predict([10.0]))

结果

Epoch 1/500
1/1 [==============================] - 2s 2s/step - loss: 0.6306
Epoch 2/500
1/1 [==============================] - 0s 7ms/step - loss: 0.6112
Epoch 3/500
1/1 [==============================] - 0s 8ms/step - loss: 0.5936
Epoch 4/500
1/1 [==============================] - 0s 8ms/step - loss: 0.5774
Epoch 5/500
1/1 [==============================] - 0s 12ms/step - loss: 0.5624
Epoch 6/500
1/1 [==============================] - 0s 9ms/step - loss: 0.5484
Epoch 7/500
1/1 [==============================] - 0s 8ms/step - loss: 0.5352
Epoch 8/500
1/1 [==============================] - 0s 11ms/step - loss: 0.5226
Epoch 9/500
1/1 [==============================] - 0s 9ms/step - loss: 0.5107
Epoch 10/500
1/1 [==============================] - 0s 10ms/step - loss: 0.4993
Epoch 11/500
1/1 [==============================] - 0s 7ms/step - loss: 0.4883
Epoch 12/500
1/1 [==============================] - 0s 8ms/step - loss: 0.4776
Epoch 13/500
1/1 [==============================] - 0s 10ms/step - loss: 0.4674
Epoch 14/500
1/1 [==============================] - 0s 11ms/step - loss: 0.4574
Epoch 15/500
1/1 [==============================] - 0s 7ms/step - loss: 0.4477
Epoch 16/500
1/1 [==============================] - 0s 9ms/step - loss: 0.4383
Epoch 17/500
1/1 [==============================] - 0s 8ms/step - loss: 0.4291
Epoch 18/500
1/1 [==============================] - 0s 10ms/step - loss: 0.4202
Epoch 19/500
1/1 [==============================] - 0s 9ms/step - loss: 0.4114
Epoch 20/500
1/1 [==============================] - 0s 7ms/step - loss: 0.4029
Epoch 21/500
1/1 [==============================] - 0s 6ms/step - loss: 0.3946
Epoch 22/500
1/1 [==============================] - 0s 6ms/step - loss: 0.3864
Epoch 23/500
1/1 [==============================] - 0s 5ms/step - loss: 0.3784
Epoch 24/500
1/1 [==============================] - 0s 7ms/step - loss: 0.3706
Epoch 25/500
1/1 [==============================] - 0s 10ms/step - loss: 0.3630
Epoch 26/500
1/1 [==============================] - 0s 7ms/step - loss: 0.3555
Epoch 27/500
1/1 [==============================] - 0s 9ms/step - loss: 0.3482
Epoch 28/500
1/1 [==============================] - 0s 11ms/step - loss: 0.3410
Epoch 29/500
1/1 [==============================] - 0s 7ms/step - loss: 0.3340
Epoch 30/500
1/1 [==============================] - 0s 10ms/step - loss: 0.3271
Epoch 31/500
1/1 [==============================] - 0s 10ms/step - loss: 0.3204
Epoch 32/500
1/1 [==============================] - 0s 9ms/step - loss: 0.3138
Epoch 33/500
1/1 [==============================] - 0s 8ms/step - loss: 0.3074
Epoch 34/500
1/1 [==============================] - 0s 8ms/step - loss: 0.3011
Epoch 35/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2949
Epoch 36/500
1/1 [==============================] - 0s 9ms/step - loss: 0.2888
Epoch 37/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2829
Epoch 38/500
1/1 [==============================] - 0s 11ms/step - loss: 0.2771
Epoch 39/500
1/1 [==============================] - 0s 8ms/step - loss: 0.2714
Epoch 40/500
1/1 [==============================] - 0s 8ms/step - loss: 0.2658
Epoch 41/500
1/1 [==============================] - 0s 9ms/step - loss: 0.2603
Epoch 42/500
1/1 [==============================] - 0s 13ms/step - loss: 0.2550
Epoch 43/500
1/1 [==============================] - 0s 9ms/step - loss: 0.2497
Epoch 44/500
1/1 [==============================] - 0s 9ms/step - loss: 0.2446
Epoch 45/500
1/1 [==============================] - 0s 11ms/step - loss: 0.2396
Epoch 46/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2347
Epoch 47/500
1/1 [==============================] - 0s 11ms/step - loss: 0.2299
Epoch 48/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2251
Epoch 49/500
1/1 [==============================] - 0s 8ms/step - loss: 0.2205
Epoch 50/500
1/1 [==============================] - 0s 8ms/step - loss: 0.2160
Epoch 51/500
1/1 [==============================] - 0s 9ms/step - loss: 0.2115
Epoch 52/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2072
Epoch 53/500
1/1 [==============================] - 0s 10ms/step - loss: 0.2029
Epoch 54/500
1/1 [==============================] - 0s 8ms/step - loss: 0.1988
Epoch 55/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1947
Epoch 56/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1907
Epoch 57/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1868
Epoch 58/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1829
Epoch 59/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1792
Epoch 60/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1755
Epoch 61/500
1/1 [==============================] - 0s 8ms/step - loss: 0.1719
Epoch 62/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1684
Epoch 63/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1649
Epoch 64/500
1/1 [==============================] - 0s 8ms/step - loss: 0.1615
Epoch 65/500
1/1 [==============================] - 0s 8ms/step - loss: 0.1582
Epoch 66/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1549
Epoch 67/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1518
Epoch 68/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1486
Epoch 69/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1456
Epoch 70/500
1/1 [==============================] - 0s 12ms/step - loss: 0.1426
Epoch 71/500
1/1 [==============================] - 0s 8ms/step - loss: 0.1397
Epoch 72/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1368
Epoch 73/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1340
Epoch 74/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1312
Epoch 75/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1285
Epoch 76/500
1/1 [==============================] - 0s 9ms/step - loss: 0.1259
Epoch 77/500
1/1 [==============================] - 0s 7ms/step - loss: 0.1233
Epoch 78/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1208
Epoch 79/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1183
Epoch 80/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1159
Epoch 81/500
1/1 [==============================] - 0s 7ms/step - loss: 0.1135
Epoch 82/500
1/1 [==============================] - 0s 7ms/step - loss: 0.1112
Epoch 83/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1089
Epoch 84/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1066
Epoch 85/500
1/1 [==============================] - 0s 12ms/step - loss: 0.1045
Epoch 86/500
1/1 [==============================] - 0s 11ms/step - loss: 0.1023
Epoch 87/500
1/1 [==============================] - 0s 10ms/step - loss: 0.1002
Epoch 88/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0982
Epoch 89/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0961
Epoch 90/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0942
Epoch 91/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0922
Epoch 92/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0903
Epoch 93/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0885
Epoch 94/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0867
Epoch 95/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0849
Epoch 96/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0831
Epoch 97/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0814
Epoch 98/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0798
Epoch 99/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0781
Epoch 100/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0765
Epoch 101/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0749
Epoch 102/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0734
Epoch 103/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0719
Epoch 104/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0704
Epoch 105/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0690
Epoch 106/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0676
Epoch 107/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0662
Epoch 108/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0648
Epoch 109/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0635
Epoch 110/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0622
Epoch 111/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0609
Epoch 112/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0596
Epoch 113/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0584
Epoch 114/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0572
Epoch 115/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0560
Epoch 116/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0549
Epoch 117/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0538
Epoch 118/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0527
Epoch 119/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0516
Epoch 120/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0505
Epoch 121/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0495
Epoch 122/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0485
Epoch 123/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0475
Epoch 124/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0465
Epoch 125/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0455
Epoch 126/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0446
Epoch 127/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0437
Epoch 128/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0428
Epoch 129/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0419
Epoch 130/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0411
Epoch 131/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0402
Epoch 132/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0394
Epoch 133/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0386
Epoch 134/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0378
Epoch 135/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0370
Epoch 136/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0362
Epoch 137/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0355
Epoch 138/500
1/1 [==============================] - 0s 14ms/step - loss: 0.0348
Epoch 139/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0341
Epoch 140/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0334
Epoch 141/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0327
Epoch 142/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0320
Epoch 143/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0313
Epoch 144/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0307
Epoch 145/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0301
Epoch 146/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0295
Epoch 147/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0288
Epoch 148/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0283
Epoch 149/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0277
Epoch 150/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0271
Epoch 151/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0265
Epoch 152/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0260
Epoch 153/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0255
Epoch 154/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0249
Epoch 155/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0244
Epoch 156/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0239
Epoch 157/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0234
Epoch 158/500
1/1 [==============================] - 0s 21ms/step - loss: 0.0230
Epoch 159/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0225
Epoch 160/500
1/1 [==============================] - 0s 20ms/step - loss: 0.0220
Epoch 161/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0216
Epoch 162/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0211
Epoch 163/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0207
Epoch 164/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0203
Epoch 165/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0199
Epoch 166/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0194
Epoch 167/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0190
Epoch 168/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0187
Epoch 169/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0183
Epoch 170/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0179
Epoch 171/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0175
Epoch 172/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0172
Epoch 173/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0168
Epoch 174/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0165
Epoch 175/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0161
Epoch 176/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0158
Epoch 177/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0155
Epoch 178/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0152
Epoch 179/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0148
Epoch 180/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0145
Epoch 181/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0142
Epoch 182/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0140
Epoch 183/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0137
Epoch 184/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0134
Epoch 185/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0131
Epoch 186/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0128
Epoch 187/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0126
Epoch 188/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0123
Epoch 189/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0121
Epoch 190/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0118
Epoch 191/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0116
Epoch 192/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0113
Epoch 193/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0111
Epoch 194/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0109
Epoch 195/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0107
Epoch 196/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0104
Epoch 197/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0102
Epoch 198/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0100
Epoch 199/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0098
Epoch 200/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0096
Epoch 201/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0094
Epoch 202/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0092
Epoch 203/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0090
Epoch 204/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0088
Epoch 205/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0087
Epoch 206/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0085
Epoch 207/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0083
Epoch 208/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0081
Epoch 209/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0080
Epoch 210/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0078
Epoch 211/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0076
Epoch 212/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0075
Epoch 213/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0073
Epoch 214/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0072
Epoch 215/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0070
Epoch 216/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0069
Epoch 217/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0067
Epoch 218/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0066
Epoch 219/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0065
Epoch 220/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0063
Epoch 221/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0062
Epoch 222/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0061
Epoch 223/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0060
Epoch 224/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0058
Epoch 225/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0057
Epoch 226/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0056
Epoch 227/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0055
Epoch 228/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0054
Epoch 229/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0053
Epoch 230/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0052
Epoch 231/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0050
Epoch 232/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0049
Epoch 233/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0048
Epoch 234/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0047
Epoch 235/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0046
Epoch 236/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0045
Epoch 237/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0045
Epoch 238/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0044
Epoch 239/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0043
Epoch 240/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0042
Epoch 241/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0041
Epoch 242/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0040
Epoch 243/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0039
Epoch 244/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0039
Epoch 245/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0038
Epoch 246/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0037
Epoch 247/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0036
Epoch 248/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0035
Epoch 249/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0035
Epoch 250/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0034
Epoch 251/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0033
Epoch 252/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0033
Epoch 253/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0032
Epoch 254/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0031
Epoch 255/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0031
Epoch 256/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0030
Epoch 257/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0029
Epoch 258/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0029
Epoch 259/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0028
Epoch 260/500
1/1 [==============================] - 0s 12ms/step - loss: 0.0028
Epoch 261/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0027
Epoch 262/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0027
Epoch 263/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0026
Epoch 264/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0025
Epoch 265/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0025
Epoch 266/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0024
Epoch 267/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0024
Epoch 268/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0023
Epoch 269/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0023
Epoch 270/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0022
Epoch 271/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0022
Epoch 272/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0022
Epoch 273/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0021
Epoch 274/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0021
Epoch 275/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0020
Epoch 276/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0020
Epoch 277/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0019
Epoch 278/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0019
Epoch 279/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0019
Epoch 280/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0018
Epoch 281/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0018
Epoch 282/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0018
Epoch 283/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0017
Epoch 284/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0017
Epoch 285/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0016
Epoch 286/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0016
Epoch 287/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0016
Epoch 288/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0015
Epoch 289/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0015
Epoch 290/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0015
Epoch 291/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0015
Epoch 292/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0014
Epoch 293/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0014
Epoch 294/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0014
Epoch 295/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0013
Epoch 296/500
1/1 [==============================] - 0s 9ms/step - loss: 0.0013
Epoch 297/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0013
Epoch 298/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0013
Epoch 299/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0012
Epoch 300/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0012
Epoch 301/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0012
Epoch 302/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0012
Epoch 303/500
1/1 [==============================] - 0s 10ms/step - loss: 0.0011
Epoch 304/500
1/1 [==============================] - 0s 7ms/step - loss: 0.0011
Epoch 305/500
1/1 [==============================] - 0s 8ms/step - loss: 0.0011
Epoch 306/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0011
Epoch 307/500
1/1 [==============================] - 0s 11ms/step - loss: 0.0010
Epoch 308/500
1/1 [==============================] - 0s 6ms/step - loss: 0.0010
Epoch 309/500
1/1 [==============================] - 0s 7ms/step - loss: 9.9976e-04
Epoch 310/500
1/1 [==============================] - 0s 10ms/step - loss: 9.7922e-04
Epoch 311/500
1/1 [==============================] - 0s 8ms/step - loss: 9.5911e-04
Epoch 312/500
1/1 [==============================] - 0s 7ms/step - loss: 9.3941e-04
Epoch 313/500
1/1 [==============================] - 0s 9ms/step - loss: 9.2011e-04
Epoch 314/500
1/1 [==============================] - 0s 10ms/step - loss: 9.0122e-04
Epoch 315/500
1/1 [==============================] - 0s 10ms/step - loss: 8.8270e-04
Epoch 316/500
1/1 [==============================] - 0s 8ms/step - loss: 8.6457e-04
Epoch 317/500
1/1 [==============================] - 0s 7ms/step - loss: 8.4681e-04
Epoch 318/500
1/1 [==============================] - 0s 11ms/step - loss: 8.2942e-04
Epoch 319/500
1/1 [==============================] - 0s 12ms/step - loss: 8.1238e-04
Epoch 320/500
1/1 [==============================] - 0s 10ms/step - loss: 7.9570e-04
Epoch 321/500
1/1 [==============================] - 0s 11ms/step - loss: 7.7935e-04
Epoch 322/500
1/1 [==============================] - 0s 7ms/step - loss: 7.6334e-04
Epoch 323/500
1/1 [==============================] - 0s 9ms/step - loss: 7.4766e-04
Epoch 324/500
1/1 [==============================] - 0s 11ms/step - loss: 7.3231e-04
Epoch 325/500
1/1 [==============================] - 0s 11ms/step - loss: 7.1726e-04
Epoch 326/500
1/1 [==============================] - 0s 11ms/step - loss: 7.0253e-04
Epoch 327/500
1/1 [==============================] - 0s 9ms/step - loss: 6.8810e-04
Epoch 328/500
1/1 [==============================] - 0s 8ms/step - loss: 6.7396e-04
Epoch 329/500
1/1 [==============================] - 0s 9ms/step - loss: 6.6012e-04
Epoch 330/500
1/1 [==============================] - 0s 12ms/step - loss: 6.4656e-04
Epoch 331/500
1/1 [==============================] - 0s 11ms/step - loss: 6.3328e-04
Epoch 332/500
1/1 [==============================] - 0s 10ms/step - loss: 6.2027e-04
Epoch 333/500
1/1 [==============================] - 0s 11ms/step - loss: 6.0753e-04
Epoch 334/500
1/1 [==============================] - 0s 8ms/step - loss: 5.9505e-04
Epoch 335/500
1/1 [==============================] - 0s 7ms/step - loss: 5.8283e-04
Epoch 336/500
1/1 [==============================] - 0s 9ms/step - loss: 5.7086e-04
Epoch 337/500
1/1 [==============================] - 0s 10ms/step - loss: 5.5913e-04
Epoch 338/500
1/1 [==============================] - 0s 11ms/step - loss: 5.4765e-04
Epoch 339/500
1/1 [==============================] - 0s 12ms/step - loss: 5.3640e-04
Epoch 340/500
1/1 [==============================] - 0s 11ms/step - loss: 5.2538e-04
Epoch 341/500
1/1 [==============================] - 0s 7ms/step - loss: 5.1459e-04
Epoch 342/500
1/1 [==============================] - 0s 11ms/step - loss: 5.0402e-04
Epoch 343/500
1/1 [==============================] - 0s 12ms/step - loss: 4.9367e-04
Epoch 344/500
1/1 [==============================] - 0s 41ms/step - loss: 4.8352e-04
Epoch 345/500
1/1 [==============================] - 0s 28ms/step - loss: 4.7359e-04
Epoch 346/500
1/1 [==============================] - 0s 9ms/step - loss: 4.6386e-04
Epoch 347/500
1/1 [==============================] - 0s 7ms/step - loss: 4.5434e-04
Epoch 348/500
1/1 [==============================] - 0s 9ms/step - loss: 4.4500e-04
Epoch 349/500
1/1 [==============================] - 0s 9ms/step - loss: 4.3586e-04
Epoch 350/500
1/1 [==============================] - 0s 8ms/step - loss: 4.2691e-04
Epoch 351/500
1/1 [==============================] - 0s 12ms/step - loss: 4.1814e-04
Epoch 352/500
1/1 [==============================] - 0s 8ms/step - loss: 4.0955e-04
Epoch 353/500
1/1 [==============================] - 0s 8ms/step - loss: 4.0114e-04
Epoch 354/500
1/1 [==============================] - 0s 8ms/step - loss: 3.9290e-04
Epoch 355/500
1/1 [==============================] - 0s 9ms/step - loss: 3.8483e-04
Epoch 356/500
1/1 [==============================] - 0s 11ms/step - loss: 3.7692e-04
Epoch 357/500
1/1 [==============================] - 0s 9ms/step - loss: 3.6918e-04
Epoch 358/500
1/1 [==============================] - 0s 7ms/step - loss: 3.6160e-04
Epoch 359/500
1/1 [==============================] - 0s 10ms/step - loss: 3.5417e-04
Epoch 360/500
1/1 [==============================] - 0s 10ms/step - loss: 3.4690e-04
Epoch 361/500
1/1 [==============================] - 0s 10ms/step - loss: 3.3977e-04
Epoch 362/500
1/1 [==============================] - 0s 9ms/step - loss: 3.3279e-04
Epoch 363/500
1/1 [==============================] - 0s 5ms/step - loss: 3.2596e-04
Epoch 364/500
1/1 [==============================] - 0s 11ms/step - loss: 3.1926e-04
Epoch 365/500
1/1 [==============================] - 0s 9ms/step - loss: 3.1271e-04
Epoch 366/500
1/1 [==============================] - 0s 11ms/step - loss: 3.0628e-04
Epoch 367/500
1/1 [==============================] - 0s 7ms/step - loss: 2.9999e-04
Epoch 368/500
1/1 [==============================] - 0s 9ms/step - loss: 2.9383e-04
Epoch 369/500
1/1 [==============================] - 0s 12ms/step - loss: 2.8779e-04
Epoch 370/500
1/1 [==============================] - 0s 12ms/step - loss: 2.8188e-04
Epoch 371/500
1/1 [==============================] - 0s 12ms/step - loss: 2.7609e-04
Epoch 372/500
1/1 [==============================] - 0s 10ms/step - loss: 2.7042e-04
Epoch 373/500
1/1 [==============================] - 0s 10ms/step - loss: 2.6487e-04
Epoch 374/500
1/1 [==============================] - 0s 7ms/step - loss: 2.5943e-04
Epoch 375/500
1/1 [==============================] - 0s 10ms/step - loss: 2.5410e-04
Epoch 376/500
1/1 [==============================] - 0s 10ms/step - loss: 2.4888e-04
Epoch 377/500
1/1 [==============================] - 0s 11ms/step - loss: 2.4377e-04
Epoch 378/500
1/1 [==============================] - 0s 10ms/step - loss: 2.3876e-04
Epoch 379/500
1/1 [==============================] - 0s 11ms/step - loss: 2.3385e-04
Epoch 380/500
1/1 [==============================] - 0s 8ms/step - loss: 2.2905e-04
Epoch 381/500
1/1 [==============================] - 0s 7ms/step - loss: 2.2435e-04
Epoch 382/500
1/1 [==============================] - 0s 11ms/step - loss: 2.1974e-04
Epoch 383/500
1/1 [==============================] - 0s 11ms/step - loss: 2.1522e-04
Epoch 384/500
1/1 [==============================] - 0s 10ms/step - loss: 2.1080e-04
Epoch 385/500
1/1 [==============================] - 0s 10ms/step - loss: 2.0647e-04
Epoch 386/500
1/1 [==============================] - 0s 7ms/step - loss: 2.0223e-04
Epoch 387/500
1/1 [==============================] - 0s 10ms/step - loss: 1.9808e-04
Epoch 388/500
1/1 [==============================] - 0s 10ms/step - loss: 1.9401e-04
Epoch 389/500
1/1 [==============================] - 0s 11ms/step - loss: 1.9002e-04
Epoch 390/500
1/1 [==============================] - 0s 9ms/step - loss: 1.8612e-04
Epoch 391/500
1/1 [==============================] - 0s 8ms/step - loss: 1.8230e-04
Epoch 392/500
1/1 [==============================] - 0s 9ms/step - loss: 1.7855e-04
Epoch 393/500
1/1 [==============================] - 0s 9ms/step - loss: 1.7489e-04
Epoch 394/500
1/1 [==============================] - 0s 10ms/step - loss: 1.7129e-04
Epoch 395/500
1/1 [==============================] - 0s 10ms/step - loss: 1.6777e-04
Epoch 396/500
1/1 [==============================] - 0s 9ms/step - loss: 1.6433e-04
Epoch 397/500
1/1 [==============================] - 0s 11ms/step - loss: 1.6095e-04
Epoch 398/500
1/1 [==============================] - 0s 7ms/step - loss: 1.5765e-04
Epoch 399/500
1/1 [==============================] - 0s 10ms/step - loss: 1.5441e-04
Epoch 400/500
1/1 [==============================] - 0s 10ms/step - loss: 1.5124e-04
Epoch 401/500
1/1 [==============================] - 0s 12ms/step - loss: 1.4813e-04
Epoch 402/500
1/1 [==============================] - 0s 9ms/step - loss: 1.4509e-04
Epoch 403/500
1/1 [==============================] - 0s 7ms/step - loss: 1.4211e-04
Epoch 404/500
1/1 [==============================] - 0s 9ms/step - loss: 1.3919e-04
Epoch 405/500
1/1 [==============================] - 0s 9ms/step - loss: 1.3633e-04
Epoch 406/500
1/1 [==============================] - 0s 12ms/step - loss: 1.3353e-04
Epoch 407/500
1/1 [==============================] - 0s 10ms/step - loss: 1.3078e-04
Epoch 408/500
1/1 [==============================] - 0s 9ms/step - loss: 1.2810e-04
Epoch 409/500
1/1 [==============================] - 0s 9ms/step - loss: 1.2547e-04
Epoch 410/500
1/1 [==============================] - 0s 12ms/step - loss: 1.2289e-04
Epoch 411/500
1/1 [==============================] - 0s 8ms/step - loss: 1.2037e-04
Epoch 412/500
1/1 [==============================] - 0s 9ms/step - loss: 1.1789e-04
Epoch 413/500
1/1 [==============================] - 0s 10ms/step - loss: 1.1547e-04
Epoch 414/500
1/1 [==============================] - 0s 10ms/step - loss: 1.1310e-04
Epoch 415/500
1/1 [==============================] - 0s 9ms/step - loss: 1.1078e-04
Epoch 416/500
1/1 [==============================] - 0s 8ms/step - loss: 1.0850e-04
Epoch 417/500
1/1 [==============================] - 0s 7ms/step - loss: 1.0627e-04
Epoch 418/500
1/1 [==============================] - 0s 11ms/step - loss: 1.0409e-04
Epoch 419/500
1/1 [==============================] - 0s 14ms/step - loss: 1.0195e-04
Epoch 420/500
1/1 [==============================] - 0s 12ms/step - loss: 9.9857e-05
Epoch 421/500
1/1 [==============================] - 0s 12ms/step - loss: 9.7806e-05
Epoch 422/500
1/1 [==============================] - 0s 10ms/step - loss: 9.5798e-05
Epoch 423/500
1/1 [==============================] - 0s 10ms/step - loss: 9.3829e-05
Epoch 424/500
1/1 [==============================] - 0s 8ms/step - loss: 9.1903e-05
Epoch 425/500
1/1 [==============================] - 0s 104ms/step - loss: 9.0014e-05
Epoch 426/500
1/1 [==============================] - 0s 9ms/step - loss: 8.8165e-05
Epoch 427/500
1/1 [==============================] - 0s 9ms/step - loss: 8.6354e-05
Epoch 428/500
1/1 [==============================] - 0s 9ms/step - loss: 8.4582e-05
Epoch 429/500
1/1 [==============================] - 0s 9ms/step - loss: 8.2844e-05
Epoch 430/500
1/1 [==============================] - 0s 11ms/step - loss: 8.1143e-05
Epoch 431/500
1/1 [==============================] - 0s 11ms/step - loss: 7.9475e-05
Epoch 432/500
1/1 [==============================] - 0s 10ms/step - loss: 7.7843e-05
Epoch 433/500
1/1 [==============================] - 0s 12ms/step - loss: 7.6245e-05
Epoch 434/500
1/1 [==============================] - 0s 11ms/step - loss: 7.4679e-05
Epoch 435/500
1/1 [==============================] - 0s 10ms/step - loss: 7.3145e-05
Epoch 436/500
1/1 [==============================] - 0s 11ms/step - loss: 7.1642e-05
Epoch 437/500
1/1 [==============================] - 0s 12ms/step - loss: 7.0170e-05
Epoch 438/500
1/1 [==============================] - 0s 11ms/step - loss: 6.8729e-05
Epoch 439/500
1/1 [==============================] - 0s 10ms/step - loss: 6.7318e-05
Epoch 440/500
1/1 [==============================] - 0s 10ms/step - loss: 6.5936e-05
Epoch 441/500
1/1 [==============================] - 0s 10ms/step - loss: 6.4581e-05
Epoch 442/500
1/1 [==============================] - 0s 8ms/step - loss: 6.3254e-05
Epoch 443/500
1/1 [==============================] - 0s 9ms/step - loss: 6.1955e-05
Epoch 444/500
1/1 [==============================] - 0s 9ms/step - loss: 6.0683e-05
Epoch 445/500
1/1 [==============================] - 0s 13ms/step - loss: 5.9436e-05
Epoch 446/500
1/1 [==============================] - 0s 12ms/step - loss: 5.8215e-05
Epoch 447/500
1/1 [==============================] - 0s 13ms/step - loss: 5.7020e-05
Epoch 448/500
1/1 [==============================] - 0s 12ms/step - loss: 5.5848e-05
Epoch 449/500
1/1 [==============================] - 0s 11ms/step - loss: 5.4701e-05
Epoch 450/500
1/1 [==============================] - 0s 13ms/step - loss: 5.3578e-05
Epoch 451/500
1/1 [==============================] - 0s 9ms/step - loss: 5.2478e-05
Epoch 452/500
1/1 [==============================] - 0s 11ms/step - loss: 5.1399e-05
Epoch 453/500
1/1 [==============================] - 0s 10ms/step - loss: 5.0344e-05
Epoch 454/500
1/1 [==============================] - 0s 9ms/step - loss: 4.9309e-05
Epoch 455/500
1/1 [==============================] - 0s 8ms/step - loss: 4.8296e-05
Epoch 456/500
1/1 [==============================] - 0s 8ms/step - loss: 4.7304e-05
Epoch 457/500
1/1 [==============================] - 0s 13ms/step - loss: 4.6333e-05
Epoch 458/500
1/1 [==============================] - 0s 11ms/step - loss: 4.5381e-05
Epoch 459/500
1/1 [==============================] - 0s 10ms/step - loss: 4.4448e-05
Epoch 460/500
1/1 [==============================] - 0s 11ms/step - loss: 4.3535e-05
Epoch 461/500
1/1 [==============================] - 0s 11ms/step - loss: 4.2641e-05
Epoch 462/500
1/1 [==============================] - 0s 10ms/step - loss: 4.1765e-05
Epoch 463/500
1/1 [==============================] - 0s 9ms/step - loss: 4.0907e-05
Epoch 464/500
1/1 [==============================] - 0s 8ms/step - loss: 4.0067e-05
Epoch 465/500
1/1 [==============================] - 0s 10ms/step - loss: 3.9245e-05
Epoch 466/500
1/1 [==============================] - 0s 11ms/step - loss: 3.8439e-05
Epoch 467/500
1/1 [==============================] - 0s 12ms/step - loss: 3.7650e-05
Epoch 468/500
1/1 [==============================] - 0s 11ms/step - loss: 3.6876e-05
Epoch 469/500
1/1 [==============================] - 0s 11ms/step - loss: 3.6118e-05
Epoch 470/500
1/1 [==============================] - 0s 10ms/step - loss: 3.5377e-05
Epoch 471/500
1/1 [==============================] - 0s 7ms/step - loss: 3.4649e-05
Epoch 472/500
1/1 [==============================] - 0s 9ms/step - loss: 3.3938e-05
Epoch 473/500
1/1 [==============================] - 0s 9ms/step - loss: 3.3240e-05
Epoch 474/500
1/1 [==============================] - 0s 10ms/step - loss: 3.2558e-05
Epoch 475/500
1/1 [==============================] - 0s 12ms/step - loss: 3.1888e-05
Epoch 476/500
1/1 [==============================] - 0s 33ms/step - loss: 3.1234e-05
Epoch 477/500
1/1 [==============================] - 0s 30ms/step - loss: 3.0593e-05
Epoch 478/500
1/1 [==============================] - 0s 9ms/step - loss: 2.9964e-05
Epoch 479/500
1/1 [==============================] - 0s 8ms/step - loss: 2.9348e-05
Epoch 480/500
1/1 [==============================] - 0s 9ms/step - loss: 2.8745e-05
Epoch 481/500
1/1 [==============================] - 0s 9ms/step - loss: 2.8155e-05
Epoch 482/500
1/1 [==============================] - 0s 10ms/step - loss: 2.7577e-05
Epoch 483/500
1/1 [==============================] - 0s 8ms/step - loss: 2.7011e-05
Epoch 484/500
1/1 [==============================] - 0s 9ms/step - loss: 2.6456e-05
Epoch 485/500
1/1 [==============================] - 0s 10ms/step - loss: 2.5913e-05
Epoch 486/500
1/1 [==============================] - 0s 11ms/step - loss: 2.5381e-05
Epoch 487/500
1/1 [==============================] - 0s 10ms/step - loss: 2.4859e-05
Epoch 488/500
1/1 [==============================] - 0s 7ms/step - loss: 2.4348e-05
Epoch 489/500
1/1 [==============================] - 0s 7ms/step - loss: 2.3848e-05
Epoch 490/500
1/1 [==============================] - 0s 12ms/step - loss: 2.3359e-05
Epoch 491/500
1/1 [==============================] - 0s 10ms/step - loss: 2.2878e-05
Epoch 492/500
1/1 [==============================] - 0s 11ms/step - loss: 2.2408e-05
Epoch 493/500
1/1 [==============================] - 0s 10ms/step - loss: 2.1948e-05
Epoch 494/500
1/1 [==============================] - 0s 7ms/step - loss: 2.1498e-05
Epoch 495/500
1/1 [==============================] - 0s 10ms/step - loss: 2.1056e-05
Epoch 496/500
1/1 [==============================] - 0s 11ms/step - loss: 2.0623e-05
Epoch 497/500
1/1 [==============================] - 0s 11ms/step - loss: 2.0199e-05
Epoch 498/500
1/1 [==============================] - 0s 12ms/step - loss: 1.9784e-05
Epoch 499/500
1/1 [==============================] - 0s 10ms/step - loss: 1.9378e-05
Epoch 500/500
1/1 [==============================] - 0s 8ms/step - loss: 1.8980e-05
[[18.987288]]

使用tensorflow2写一个简单的线性回归模型的训练与预测_第1张图片

作业:预测房屋价格

介绍

a   h o u s e   h a s   a   b a s e   c o s t   o f   50 k , a n d   e v e r y   a d d i t i o n a l   b e d r o o m   a d d s   a   c o s t   o f   100 k , a   2   b e d r o o m   h o u s e   c o s t   150 k   e t c . a\ house\ has\ a\ base\ cost\ of\ 50k,and\ every\ additional\ bedroom\\\ adds\ a\ cost\ of\ 100k,a\ 2\ bedroom\ house\ cost\ 150k\ etc. a house has a base cost of 50k,and every additional bedroom adds a cost of 100k,a 2 bedroom house cost 150k etc.

  • 解析
    房屋价格基准是 50 k 50k 50k,每增加1个 b e d r o o m bedroom bedroom就增加 50 k 50k 50k,然后输入 b e d r o o m bedroom bedroom的个数,输出房屋价格。
  • 注意
    设计 y s ys ys时,最好将 400 k 400k 400k这样的大数置为 4 4 4

代码

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Dense
from tensorflow.keras import Sequential

def house_model():
    xs = np.array([0,1,2,3,4,5,],dtype=float)
    ys = np.array([0.5,1,1.5,2.0,2.5,3.0,],dtype=float)

    model = Sequential(
        [
            Dense(units=1,input_shape=[1]),

        ]
    )

    model.compile(optimizer='sgd',loss='mse')

    model.fit(xs,ys,epochs=1000)

    return model

if __name__ == '__main__':
    model = house_model()

    new_x = 7.0
    prediction = model.predict([new_x])[0]
    print(prediction)

结果

50 + 7 × 50 = 400 50+7\times50=400 50+7×50=400,也即意味着应该输出4左右的值。

在这里插入图片描述

你可能感兴趣的:(tensorflow2,深度学习,tensorflow,线性回归,深度学习)