0%

MachineProblem

机器学习学习过程中出现的问题

keras和sklearn联合使用

tensorflow keras 混合编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from keras.layers import Input, Dense, Dropout, BatchNormalization,\
LeakyReLU, concatenate,Conv2D, MaxPooling2D,\
AveragePooling2D, GlobalAveragePooling2D,Conv1D,\
Flatten,merge,Reshape,MaxPooling1D,Lambda,LSTM,\
GlobalMaxPooling2D

# build module

img = tf.placeholder(tf.float32, shape=(None, 784))
labels = tf.placeholder(tf.float32, shape=(None, 10))

x = Dense(128, activation='relu')(img)
x = Dense(128, activation='relu')(x)
prediction = Dense(10, activation='softmax')(x)

loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=prediction, labels=labels))

train_optim = tf.train.AdamOptimizer().minimize(loss)

mnist_data = input_data.read_data_sets('MNIST_data/', one_hot=True)

with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
batch_x, batch_y = mnist_data.train.next_batch(50)
sess.run(train_optim, feed_dict={img: batch_x, labels: batch_y})

acc_pred = tf.keras.metrics.categorical_accuracy(labels, prediction)
pred = sess.run(acc_pred, feed_dict={labels: mnist_data.test.labels, img: mnist_data.test.images})

print('accuracy: %.3f' % (sum(pred)/len(mnist_data.test.labels)))
```
## [keras 输出中间层](https://blog.csdn.net/tszupup/article/details/89192737)
1 通过K.function()函数打印模型中间层的输出
2 通过函数API打印模型中间层的输出

## 训练过程可视化

```python
import matplotlib.pyplot as plt

history = model.fit(x, y, validation_split=0.25, epochs=50, batch_size=16, verbose=1)

# 绘制训练 & 验证的准确率值
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# 绘制训练 & 验证的损失值
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

keras训练多输入

https://www.jb51.net/article/189193.htm

Cuda10 RTX2070 安装
同一系统下安装不同的Cuda版本
Cuda版本
CudaNN对应版本

Python小知识

[1]class继承类重写类的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classFoo(object):
def__new__(cls,*args,**kwargs):
obj=object.__new__(cls,*args,**kwargs)
# 这里的object.__new__(cls, *args, **kwargs) 等价于
# super(Foo, cls).__new__(cls, *args, **kwargs)
# object.__new__(Foo, *args, **kwargs)
# Bar.__new__(cls, *args, **kwargs)
# Student.__new__(cls, *args, **kwargs),即使Student跟Foo没有关系,也是允许的,因为Student是从object派生的新式类

# 在任何新式类,不能调用自身的“__new__”来创建实例,因为这会造成死循环
# 所以要避免return Foo.__new__(cls, *args, **kwargs)或return cls.__new__(cls, *args, **kwargs)
print"Call __new__ for %s"%obj.__class__
returnobj

classBar(Foo):
def__new__(cls,*args,**kwargs):
obj=object.__new__(cls,*args,**kwargs)
print"Call __new__ for %s"%obj.__class__
returnobj

[2]super()函数是用于调用父类(超类)的一个方法。

1
2
3
4
5
6
7
8
9
class A:
def add(self, x):
y = x+1
print(y)
class B(A):
def add(self, x):
super().add(x)
b = B()
b.add(2) # 3

python问题集锦

anaconda特定环境下运行jupyter notebook

1
2
3
4
$ conda activate ENV  #启动相应环境
$ conda install jupyter
$ conda install ipython
$ jupyter notebook #启动jupyter notebook
坚持原创技术分享,您的支持将鼓励我继续创作!