2.14,多输入,多输出

核心

#多输入
input_wide=keras.layers.Input(shape=[5])
input_deep=keras.layers.Input(shape=[6])
hidden1=keras.layers.Dense(30,activation='relu')(input_deep)
hidden2=keras.layers.Dense(30,activation='relu')(hidden1)
concat=keras.layers.concatenate([input_wide,hidden2])
output=keras.layers.Dense(1)(concat)
output2=keras.layers.Dense(1)(hidden2)
#model=keras.models.Model(inputs=[input_wide,input_deep],outputs=[output])
model=keras.Model(inputs=[input_wide,input_deep],
                  outputs=[output,output2])
model.compile(loss='mean_squared_error',optimizer='sgd')
callbacks=[keras.callbacks.EarlyStopping(patience=5,min_delta=1e-2)]

x_train_scaled_wide=x_train_scaled[:,:5]
x_train_scaled_deep=x_train_scaled[:,2:]
x_valid_scaled_wide=x_valid_scaled[:,:5]
x_valid_scaled_deep=x_valid_scaled[:,2:]
x_test_scaled_wide=x_test_scaled[:,:5]
x_test_scaled_deep=x_test_scaled[:,2:]

history=model.fit([x_train_scaled_wide,x_train_scaled_deep],
                  [y_train,y_train],
                  validation_data=(
                      [x_valid_scaled_wide,x_valid_scaled_deep],
                      [y_valid,y_valid]),
                  epochs=1,callbacks=callbacks)

全部

import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time

from sklearn.datasets import fetch_california_housing

housing=fetch_california_housing()
'''
#打印数据
import pprint
pprint.pprint(housing.data[0:5])
pprint.pprint(housing.target[0:5])
'''
from sklearn.model_selection import train_test_split

x_train_all,x_test,y_train_all,y_test=train_test_split(housing.data,housing.target,random_state=11,test_size=0.3)
x_train,x_valid,y_train,y_valid=train_test_split(x_train_all,y_train_all,random_state=11)

#归一化
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
x_train_scaled=scaler.fit_transform(x_train)
x_valid_scaled=scaler.transform(x_valid)
x_test_scaled=scaler.transform(x_test)

#多输入
input_wide=keras.layers.Input(shape=[5])
input_deep=keras.layers.Input(shape=[6])
hidden1=keras.layers.Dense(30,activation='relu')(input_deep)
hidden2=keras.layers.Dense(30,activation='relu')(hidden1)
concat=keras.layers.concatenate([input_wide,hidden2])
output=keras.layers.Dense(1,name='output_1')(concat)
output2=keras.layers.Dense(1,name='output_2')(hidden2)
#model=keras.models.Model(inputs=[input_wide,input_deep],outputs=[output])
model=keras.Model(inputs=[input_wide,input_deep],
                  outputs=[output,output2])
model.compile(loss='mean_squared_error',optimizer='sgd',metrics=['mae'])
callbacks=[keras.callbacks.EarlyStopping(patience=5,min_delta=1e-2)]


x_train_scaled_wide=x_train_scaled[:,:5]
x_train_scaled_deep=x_train_scaled[:,2:]
x_valid_scaled_wide=x_valid_scaled[:,:5]
x_valid_scaled_deep=x_valid_scaled[:,2:]
x_test_scaled_wide=x_test_scaled[:,:5]
x_test_scaled_deep=x_test_scaled[:,2:]

history=model.fit([x_train_scaled_wide,x_train_scaled_deep],
                  [y_train,y_train],
                  validation_data=(
                      [x_valid_scaled_wide,x_valid_scaled_deep],
                      [y_valid,y_valid]),
                  epochs=1,callbacks=callbacks)
test_score=model.evaluate([x_test_scaled_wide,x_test_scaled_deep],[y_test,y_test])
#打印出每个参数是什么意思
print(model.metrics_names)
print(test_score)

但是出现了error,我也不知道为啥,等我懂了再来补坑
Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[-3.40638696e-01, 4.12873275e-01, 3.40608114e-02, …,

原因:
我注意了模型的搭建,但是我忽视了evaluate的输入

你可能感兴趣的:(tensorflow2.0)