Java可序列化和Lambda表达式

问题描述

我需要将一个对象保存在文件中,然后在以后检索它。对象本身实现接口# split into train-test set size = int(len(X) * 0.75) train,test = hospitalization_diff[:size],hospitalization_diff[size:] # Build Model model = ARIMA(train,order=(0,1)) fitted = model.fit(disp=-1) # Forecast fc,se,conf = fitted.forecast(len(test),alpha=0.05) # 95% conf # Make as pandas series fc_series = pd.Series(fc,index=test.index) lower_series = pd.Series(conf[:,0],index=test.index) upper_series = pd.Series(conf[:,1],index=test.index) # Plot plt.figure(figsize=(12,5),dpi=100) plt.plot(train,label='training') plt.plot(test,label='actual') plt.plot(fc_series,label='forecast') plt.fill_between(lower_series.index,lower_series,upper_series,color='k',alpha=.15) plt.title('Forecast vs Actuals') plt.legend(loc='upper left',fontsize=8) plt.show() ,但是其字段之一包含lambda表达式。显然,这算作一个未实现Serializable接口的字段,我得到了Serializable

我不想彻底更改我的代码,但是我不知道在这种情况下该怎么做。有人有建议吗?

以下是重复此问题的示例代码

java.io.NotSerializableException

}

解决方法

您不能保留一个函数-它不代表数据,它代表行为。 使用# importing libraries import tkinter as tk from tkinter import * # import PIL from tkinter import filedialog import numpy from PIL import Image,ImageTk import torch # importing model model=torch.load('corelK_model_0.pt') classes = { 0:'africa',1: 'beach',2: 'tallbuilding',3: 'buses',4: 'dinosaurs',5: 'elephants',6: 'Roses',7: 'horses',8: 'mountains',9: 'food' } def upload_image(): file_path = filedialog.askopenfilename() uploaded = Image.open(file_path) uploaded.thumbnail(((top.winfo_width() / 2.25,(top.winfo_height() / 2.25)))) im = ImageTk.PhotoImage(uploaded) sign_image.configure(image=im) sign_image.image = im label.configure(text=' ') show_classify_button(file_path) def show_classify_button(file_path): classify_btn = Button(top,text="Classify Image",command=lambda: classify(file_path),padx=10,pady=5) classify_btn.configure(background="#364156",foreground="white",font=('arial',10,'bold')) classify_btn.place(relx=0.79,rely=0.46) def classify(file_path): image = Image.open(file_path) image = image.resize((32,32)) image = numpy.expand_dims(image,axis=0) image = numpy.array(image) pred = model.predict_classes([image])[0] sign = classes[pred] print(sign) label.configure(foreground='#011638',text=sign) # initialize GUI top = tk.Tk() # calling the constructor or creating the object of tk class top.geometry('800x600') # set height and width top.title("Image Classification CIFAR10") top.configure(background="#CDCDCD") # set Heading heading = Label(top,text="Image Classifier",pady=20,20,'bold')) heading.configure(background="#CDCDCD",foreground='#364156') heading.pack() upload = Button(top,text="Upload an image",command=upload_image,pady=5) heading.configure(background="#364156",foreground='white','bold')) upload.pack(side=BOTTOM,pady=50) # upload image sign_image = Label(top) sign_image.pack(side=BOTTOM,expand=True) # predicted class label = Label(top,background="#CDCDCD",15,'bold')) label.pack(side=BOTTOM,expand=True) top.mainloop() 关键字可以防止其在文件中持续存在。

transient

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...