让我看看是哪个蠢货,拜托朋友安装了tensorflow(废了好几个小时),结果现在一点也不会的(没错,正是在下)(这是干什么的,其实我也不太清楚,就安装了)(啊我真的服了我自己)(干脆我撞墙得了)(平静发疯中)
好像还是安装了最新版本,朋友还给设置了一个虚拟环境,总之相当麻烦(这就是我比较喜欢matlab的原因,而且我记得之前好像还安装到了2.14版本,总之很混乱)然后再某乎复制了代码,跑了一下,感觉好牛的样子。
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
print(tf.__version__)
# 加载数据、区分出测试数据和训练数据
# 注意:如果已经下过,重复下载的话,可能会出现EOFError: Compressed file ended before the end-of-stream marker was reached错误
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# 分类的列表
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# 查看数据的值
print(train_images.shape) # 样本的shape(可以看出有多少条样本、维度)
print(len(train_labels)) # label的个数,有多少个样本,就应该有多少个label
print(train_labels) # label的值,对应上面分类列表(从0到9)
# 对测试数据和训练数据进行预处理(实际上就是归一化)
train_images = train_images / 255.0
test_images = test_images / 255.0
# 设置层 (初始处理)--- 建立神经层
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
# 损失函数、优化器、指标
model.compile(optimizer = tf.optimizers.Adam(),
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
# 将训练集丢进去,训练出模型(Model)
model.fit(train_images, train_labels, epochs=5)
# 将测试数据丢到模型中,评估一下得分(准确率)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
# 评估完准确率以后,我们可以对测试数据进行预测
predictions = model.predict(test_images)
# 选第一个样本预测后的得出最有可能结果
print(np.argmax(predictions[0]))
# 对比结果
print(test_labels[0])
运行结果:
2.10.0
(60000, 28, 28)
60000
[9 0 0 ... 3 0 5]
Epoch 1/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4936 - accuracy: 0.8264
Epoch 2/5
1875/1875 [==============================] - 8s 4ms/step - loss: 0.3697 - accuracy: 0.8645
Epoch 3/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3334 - accuracy: 0.8788
Epoch 4/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3111 - accuracy: 0.8848
Epoch 5/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.2923 - accuracy: 0.8924
313/313 [==============================] - 1s 3ms/step - loss: 0.3456 - accuracy: 0.8772
Test accuracy: 0.8772000074386597
313/313 [==============================] - 1s 2ms/step
9
9
从零开始学TensorFlow - 知乎 (zhihu.com)
此外遇到过一个问题
AttributeError: module 'tensorflow._api.v2.train' has no attribute 'AdamOptimizer' 具体解决方案可以参考module ‘tensorflow._api.v2.train‘ has no attribute ‘AdamOptimizer‘ (不必更改tensorflow 2)_module 'tensorflow._api.v2.train' has no attribute-CSDN博客
今天还翻出来这个代码,好久没看,我都想不起这是什么玩意了(哭,我真是个five)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
DNA_SIZE = 24
POP_SIZE = 200
CROSSOVER_RATE = 0.8
MUTATION_RATE = 0.005
N_GENERATIONS = 50
X_BOUND = [-3, 3]
Y_BOUND = [-3, 3]
def F(x, y):
return 3 * (1 - x) ** 2 * np.exp(-(x ** 2) - (y + 1) ** 2) - 10 * (x / 5 - x ** 3 - y ** 5) * np.exp(
-x ** 2 - y ** 2) - 1 / 3 ** np.exp(-(x + 1) ** 2 - y ** 2)
def plot_3d(ax):
X = np.linspace(*X_BOUND, 100)
Y = np.linspace(*Y_BOUND, 100)
X, Y = np.meshgrid(X, Y)
Z = F(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm)
ax.set_zlim(-10, 10)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.pause(3)
plt.show()
def get_fitness(pop):
x, y = translateDNA(pop)
pred = F(x, y)
return (pred - np.min(pred)) # 减去最小的适应度是为了防止适应度出现负数,通过这一步fitness的范围为[0, np.max(pred)-np.min(pred)]
def translateDNA(pop): # pop表示种群矩阵,一行表示一个二进制编码表示的DNA,矩阵的行数为种群数目
x_pop = pop[:, 1::2] # 奇数列表示X,从索引列1开始,加入了步长2
y_pop = pop[:, ::2] # 偶数列表示y,从索引列1开始,加入了步长2
# pop:(POP_SIZE,DNA_SIZE)*(DNA_SIZE,1) --> (POP_SIZE,1)
x = x_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (X_BOUND[1] - X_BOUND[0]) + X_BOUND[0]
y = y_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (Y_BOUND[1] - Y_BOUND[0]) + Y_BOUND[0]
return x, y
def crossover_and_mutation(pop, CROSSOVER_RATE=0.8):
new_pop = []
for father in pop: # 遍历种群中的每一个个体,将该个体作为父亲
child = father # 孩子先得到父亲的全部基因(这里我把一串二进制串的那些0,1称为基因)
if np.random.rand() < CROSSOVER_RATE: # 产生子代时不是必然发生交叉,而是以一定的概率发生交叉
mother = pop[np.random.randint(POP_SIZE)] # 再种群中选择另一个个体,并将该个体作为母亲
cross_points = np.random.randint(low=0, high=DNA_SIZE * 2) # 随机产生交叉的点
child[cross_points:] = mother[cross_points:] # 孩子得到位于交叉点后的母亲的基因
mutation(child) # 每个后代有一定的机率发生变异
new_pop.append(child)
return new_pop
def mutation(child, MUTATION_RATE=0.003):
if np.random.rand() < MUTATION_RATE: # 以MUTATION_RATE的概率进行变异
mutate_point = np.random.randint(0, DNA_SIZE * 2) # 随机产生一个实数,代表要变异基因的位置
child[mutate_point] = child[mutate_point] ^ 1 # 将变异点的二进制为反转
def select(pop, fitness): # nature selection wrt pop's fitness
idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True,
p=(fitness) / (fitness.sum()))
return pop[idx]
def print_info(pop):
fitness = get_fitness(pop)
max_fitness_index = np.argmax(fitness)
print("max_fitness:", fitness[max_fitness_index])
x, y = translateDNA(pop)
print("最优的基因型:", pop[max_fitness_index])
print("(x, y):", (x[max_fitness_index], y[max_fitness_index]))
if __name__ == "__main__":
fig = plt.figure()
ax = Axes3D(fig)
plt.ion() # 将画图模式改为交互模式,程序遇到plt.show不会暂停,而是继续执行
plot_3d(ax)
pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2)) # matrix (POP_SIZE, DNA_SIZE)
for _ in range(N_GENERATIONS): # 迭代N代
x, y = translateDNA(pop)
if 'sca' in locals():
sca.remove()
sca = ax.scatter(x, y, F(x, y), c='black', marker='o')
plt.show()
plt.pause(0.1)
pop = np.array(crossover_and_mutation(pop, CROSSOVER_RATE))
# F_values = F(translateDNA(pop)[0], translateDNA(pop)[1])#x, y --> Z matrix
fitness = get_fitness(pop)
pop = select(pop, fitness) # 选择生成新的种群
print_info(pop)
plt.ioff()
plot_3d(ax)
max_fitness: 0.34314062949922874
最优的基因型: [1 0 1 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 0 1 0 1 1 1 1 0 1 0 0 1 1
1 1 0 1 1 1 1 1 0 0 0]
(x, y): (-0.016257585064028746, 1.558936629231967)
PS:我还拜托了朋友在我pycharm安装了这个
import sklearn help(sklearn)
这个就更陌生了啊喂(抓狂)
VERSION
1.0.2
import numpy as np
import matplotlib.pyplot as plt
#根据模型的定义产生模拟样本
x = np.matrix(range(26,140)).reshape(-1,1)
m = x.shape[0]
print(x)
y = x*3 + 20 + np.random.normal(0,9,(m,1))
print(y)
x_orignal = list(x); y_orignal = list(y)
# plt.axis([0,160, 0,50])
plt.scatter(x_orignal,y_orignal)
plt.xlabel("size")
plt.ylabel("House Price")
plt.title("Scatter")
plt.show()
x0 = np.ones((m,1)) ### 向量化
X = np.hstack((x0,x))
Y = y
print(X)
print(Y)
def predict(X, theta):
y_hat = np.dot(X,theta)
return y_hat
def loss(theta, X, Y):
error = predict(X, theta)-Y
loss_value=np.mean(np.square(error))
return loss_value
def derivative(theta, X, Y):
error = predict(X, theta)-Y
deriv = np.multiply(error,X)
deriv_mean =np.mean(deriv,axis=0)
return deriv_mean.T
len_theta = 2
alhpa=0.00002# 0.001; 0.0001 ; 0.0000001, 3
iter_num= 100
theta = np.zeros((len_theta,1))
loss_vector= np.zeros((iter_num,1))
theta_vector = np.zeros((len_theta,iter_num))
print(theta)
print(theta_vector)
for i in range(iter_num):
theta_vector[:,i] = theta.T
theta = theta-alhpa*derivative(theta, X, Y) # 等价 theta += -alhpa*derivative(theta)
loss_vector[i] = loss(theta, X,Y)
plt.plot(range(1,(iter_num+1)), loss_vector )
plt.xlabel("Number of iterations")
plt.ylabel("Loss function")
plt.title("Train example")
plt.show()
这段代码是一个简单的线性回归示例,用于拟合一个线性模型来预测房价。下面是对代码的解释:
1. 首先,根据模型的定义生成模拟样本。这里生成了一个x的矩阵,然后根据线性关系 y = 3x + 20 + 噪声 来生成对应的y值。
2. 将生成的样本数据进行可视化,以散点图的形式展示在图上,x轴为房屋大小,y轴为房价。
3. 对数据进行预处理,将x向量化并添加偏置项,构建设计矩阵X和目标变量Y。
4. 定义了预测函数predict,用于预测模型的输出值。
5. 定义了损失函数loss,用于计算模型的损失值,这里使用均方误差作为损失函数。
6. 定义了梯度函数derivative,用于计算损失函数对参数的梯度。
7. 初始化模型参数theta,学习率alpha,迭代次数iter_num,损失值向量loss_vector和参数向量theta_vector。
8. 使用梯度下降算法更新模型参数theta,不断迭代直到达到指定的迭代次数。
9. 绘制损失函数随迭代次数变化的曲线,用于观察模型训练的效果。
这段代码演示了一个简单的线性回归模型训练过程,通过梯度下降算法不断优化模型参数,使得模型能够更好地拟合数据。
想都不要想我把这个代码改复杂了。。。
知识它在我脑子里面打架啊喂
import numpy as np
import pandas as pd
pd.set_option('display.max_columns',1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth',1000)#文件读取行列多一些,不会省略
import os
os.getcwd()
dataset = pd.read_csv(r"C:/Users/这边输入自己的文件存储路径即可Desktop/HousePrice1.csv", encoding = "gbk")
print(dataset.head())#表头
print(dataset.tail())#表尾
print(type(dataset))
print(dataset.ndim)
print(dataset.shape)
print(type(dataset.iloc[:,3]))
print(dataset.iloc[:,3])
print(dataset.iloc[:,3].str[0:-2] [0])
x = dataset.iloc[:,3].str[0:-2]
x = x.astype(float)
print(x)
print(dataset.iloc[:,10].str[:-1].astype(float))
m=2000
dataset2 = dataset.sample(n=m, axis=0, random_state=0)# axis 表示对哪个方向随机取样,或下面抽取 # help(dataset.sample) 可查询函数用法
print(dataset2)
# sampleindex = np.random.choice(range(3000),2000,replace=False)
# data_sample= data.iloc[sampleindex]
print(dataset2.head())
y=dataset2.iloc[:,3].str[0:-2]
print(dataset2.iloc[:,3].str[0:-2])
y = np.matrix(dataset2.iloc[:,3].str[:-2])
y=y.T
print(y)
y = y.astype(float) # 将x转换为数值型
print(y)
z=dataset2.iloc[:,10].str[0:-1]
print(z)
z = z.astype(float).T
print(z)
m=2000
dataset = pd.read_csv(r"C:/Users/和上面一样的存储路径Desktop/HousePrice1.csv", encoding = "gbk") ##
dataset2 = dataset.sample(n=m, axis=0, random_state=0) # axis 表示对哪个方向随机取样,或下面抽取
x, y = dataset2.iloc[:,3].str[0:-2], dataset2.iloc[:,10].str[0:-1]
x, y = np.matrix(x.astype("float64")).T, np.matrix(y.astype("float64")).T
x0, x2, x3 = np.ones((m,1)), np.power(x,2), np.power(x,3)
X = np.hstack((x, x2, x3))
X_mean, X_std = np.mean(X,axis=0), np.std(X,axis=0)
X = np.hstack( (x0, (X-X_mean)/X_std ) )
print(X)
还真能运行出来hhhhhh,就是把excel存为csv,然后读取。
垃圾和知识,基本就先整理到这儿。