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的输入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值