You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R环境使用Keras训练模型调用fit()时报py_call_impl错误求助

问题根因

你代码的核心问题是输入特征维度和模型输入层定义的维度不匹配:

  • 你构造的输入特征矩阵xn一共绑定了7个字段(L1SW/L2SW/L3SW/BOAMSW/RFSW/ET/ST),单条样本的特征维度是7
  • 但你定义模型第一层的input_shape参数填了3,和实际输入维度不一致,直接触发了训练时的ValueError

解决方案

按如下步骤修改代码即可正常运行:

  1. 将第一层全连接层的input_shape参数从3改为7,匹配实际输入的特征维度
  2. 训练前将xn、yn显式转为矩阵类型,适配keras的数据输入要求

修正后的完整代码

df<-read.csv("https://raw.githubusercontent.com/stchln/test/main/prtt.csv",sep=",")
xn<-cbind(df$L1SW,df$L2SW,df$L3SW,df$BOAMSW,df$RFSW,df$ET,df$ST)
yn<-cbind(df$pronto_id)
# 转换为矩阵适配keras输入要求
xn <- as.matrix(xn)
yn <- as.matrix(yn)

library(keras)
model = keras_model_sequential() %>% 
   # 修正input_shape为实际特征数7
   layer_dense(units=64, activation="relu", input_shape=7) %>% 
   layer_dense(units=32, activation = "relu") %>% 
   layer_dense(units=1, activation="linear")
 
model %>% compile(
   loss = "mse",
   optimizer =  "adam", 
   metrics = list("mean_absolute_error")
 )
model %>% summary()

model %>% fit(xn, yn, epochs = 100,verbose = 0)

内容的提问来源于stack exchange,提问作者stchln

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 13:24:05