import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import time
# # 加载鸢尾花数据集
# iris=load_iris()
# x=iris.data
# y=iris.target
# x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42,shuffle=True)
# # 归一化数据,神经网络对于输入数据的尺寸敏感,归一化是最常见的处理方式
# ##自变量有多个,且量纲不尽相同对其进行归一化减少量纲对结果的影响
# from sklearn.preprocessing import MinMaxScaler
# scaler=MinMaxScaler()
# x_train=scaler.fit_transform(x_train)
# x_test=scaler.transform(x_test)
# # 将数据转换为 PyTorch 张量,因为 PyTorch 使用张量进行训练
# # y_train和y_test是整数,所以需要转化为long类型,如果是float32,会输出1.0 0.0
# x_train=torch.FloatTensor(x_train)
# y_train=torch.LongTensor(y_train)
# x_test=torch.FloatTensor(x_test)
# y_train=torch.LongTensor(y_train)
# class MLP(nn.Module):
# def __init__(self):
# super(MLP,self).__init__()
# self.fc1=nn.Linear(4,10)
# self.relu=nn.ReLU()
# self.fc2=nn.Linear(10,3)
# def forward(self,x):
# x=self.relu(self.fc1(x))
# x=self.fc2(x)
# return x
# model=MLP()
# #交叉熵损失函数
# criterion=nn.CrossEntropyLoss()
# optimizer=optim.Adam(model.parameters(),lr=0.001)
# num_epochs=20000
# losses=[]
# start_time=time.time()
# for epoch in range(num_epochs):
# outputs=model.forward(x_train)
# loss=criterion(outputs,y_train)
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# losses.append(loss.item())
# if(epoch+1)%1000==0:
# print(f'epoch[{epoch+1}/{num_epochs}],loss:{loss.item():.4f}')
# time_all=time.time()-start_time
# print(f'Training time: {time_all:.2f} seconds')
# plt.plot(range(num_epochs),losses)
# plt.xlabel('Epoch')
# plt.ylabel('Loss')
# plt.title('Training Loss over Epochs')
# plt.show()
# ##GPU
# if torch.backends.mps.is_available():
# device=torch.device('mps')
# else:
# device=torch.device('cpu')
# iris=load_iris()
# x=iris.data
# y=iris.target
# x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=42,shuffle=True)
# # # 归一化数据,神经网络对于输入数据的尺寸敏感,归一化是最常见的处理方式
# # ##自变量有多个,且量纲不尽相同对其进行归一化减少量纲对结果的影响
# from sklearn.preprocessing import MinMaxScaler
# scaler=MinMaxScaler()
# x_train=scaler.fit_transform(x_train)
# x_test=scaler.fit_transform(x_test)
# # 将数据转换为PyTorch张量并移至GPU
# # 分类问题交叉熵损失要求标签为long类型
# # 张量具有to(device)方法,可以将张量移动到指定的设备上
# x_train=torch.FloatTensor(x_train).to(device)
# y_train=torch.LongTensor(y_train).to(device)
# x_test=torch.FloatTensor(x_test).to(device)
# y_test=torch.LongTensor(y_test).to(device)
# class MLP(nn.Module):
# def __init__(self):
# super(MLP,self).__init__()
# self.fc1=nn.Linear(4,10)
# self.relu=nn.ReLU()
# self.fc2=nn.Linear(10,3)
# def forward(self,x):
# x=self.relu(self.fc1(x))
# x=self.fc2(x)
# return x
# # 实例化模型并移至GPU
# # MLP继承nn.Module类,所以也具有to(device)方法
# model=MLP().to(device)
# criterion=nn.CrossEntropyLoss()
# optimizer=optim.Adam(model.parameters(),lr=0.001)
# num_epochs=20000
# losses=[]
# start_time=time.time()
# for epoch in range(num_epochs):
# outputs=model(x_train)
# loss=criterion(outputs,y_train)
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# # losses.append(loss.item())
# if (epoch + 1) % 1000 == 0:
# print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
# time_all = time.time() - start_time
# print(f'Training time: {time_all:.2f} seconds')
# # plt.plot(range(num_epochs),losses)
# # plt.xlabel('Epoch')
# # plt.ylabel('Loss')
# # plt.title('Training Loss over Epochs')
# # plt.show()
# class Counter:
# def __init__(self):
# self.count=0
# self.a=6
# def __call__(self):
# self.count+=1
# return self.count
# count=Counter()
# print(count())
# print(count.a)
class Adder:
def __init__(self):
self.name=520
def __call__(self,a,b,name=555):
return (f'{name}:{a+b}')
adder=Adder()
print(adder(3,5,name=666))