-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnot_for_git_Aufgabe6.1.py
More file actions
134 lines (108 loc) · 2.88 KB
/
not_for_git_Aufgabe6.1.py
File metadata and controls
134 lines (108 loc) · 2.88 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 15:49:52 2019
@author: mikhail
"""
import bilderGenerator as bG
import numpy as np
from skimage.io import imread, imsave
from skimage.filters import sobel_h, sobel_v, threshold_otsu, gaussian
#import Funktionsscript as my
from skimage.measure import regionprops
import matplotlib.pyplot as plt
#1.1
tr_mws, tr_stds, tr_labels = bG.zieheBilder(250)
vl_mws, vl_stds, vl_labels = bG.zieheBilder(25)
#1.2
'''
plt.close('all')
fig, ax = plt.subplots(1,1)
index_k1 = np.where(tr_labels==1)
tr_mws_k1=tr_mws[index_k1]
tr_stds_k1=tr_stds[index_k1]
ax.plot(tr_mws_k1,tr_stds_k1,'rx')
index_k2 = np.where(tr_labels==-1)
tr_mws_k2=tr_mws[index_k2]
tr_stds_k2=tr_stds[index_k2]
ax.plot(tr_mws_k2,tr_stds_k2,'rx')
'''
#1.3
w1=0.0001
w2=-0.0002
b=0.001
tr_y = []
for t in list(range(0,500,1)):
tr_y.append(w1 *tr_mws[t] + w2*tr_stds[t] + b)
w1=0.0001
w2=-0.0002
b=0.001
vl_y = []
for t in list(range(0,50,1)):
y=w1 *vl_mws[t] + w2*vl_stds[t] + b
if y>=0:
y=1
else:
y=-1
vl_y.append(y)
hit=0
for t in list(range(0,50,1)):
if(vl_y[t]==vl_labels[t]):
hit+=1
print(hit/len(vl_labels))
#1.4
#initialisierung
w1=np.random.normal(0,0.001)
w2=np.random.normal(0,0.001)
b=0
hitrates=[]
for t in list(range(0,100,1)):
tr_y = []
for t in list(range(0,500,1)):
y=w1 *tr_mws[t] + w2*tr_stds[t] + b
if(np.sign(y)==np.sign(tr_labels[t])):
continue
else:
#partielle Ableitungen bestimmen, Formel siehe Folie
d_w1=2*(w1 * tr_mws[t] + w2 * tr_stds[t] + b - tr_labels[t]) * tr_mws[t]
d_w2=2*(w1 * tr_mws[t] + w2 * tr_stds[t] + b - tr_labels[t]) * tr_stds[t]
d_b=2*(w1 * tr_mws[t] + w2 * tr_stds[t] + b - tr_labels[t])
#Gewichte aktualisiern
learning_rate=0.0000005
w1=w1-learning_rate*d_w1
w2=w2-learning_rate*d_w2
b=b-learning_rate*d_b
vl_y = []
for t in list(range(0,50,1)):
y=w1 *vl_mws[t] + w2*vl_stds[t] + b
if y>=0:
y=1
else:
y=-1
vl_y.append(y)
hit=0
for t in list(range(0,50,1)):
if(vl_y[t]==vl_labels[t]):
hit+=1
hitrates.append(hit/len(vl_labels))
print(hitrates)
#1.7
line_x=[]
line_y=[]
for x in list(range(0,255,1)):
for y in list(range(0,128,1)):
y_end=w1 *x + w2*y + b
if y_end<0.0001:
line_x.append(x)
line_y.append(y)
plt.close('all')
fig, ax = plt.subplots(1,1)
index_k1 = np.where(tr_labels==1)
tr_mws_k1=tr_mws[index_k1]
tr_stds_k1=tr_stds[index_k1]
ax.plot(tr_mws_k1,tr_stds_k1,'rx')
index_k2 = np.where(tr_labels==-1)
tr_mws_k2=tr_mws[index_k2]
tr_stds_k2=tr_stds[index_k2]
ax.plot(tr_mws_k2,tr_stds_k2,'bx')
ax.plot(line_x,line_y,'gx')