-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtroubleShooting.py
171 lines (128 loc) · 4 KB
/
troubleShooting.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 18 16:11:01 2021
@author: Employee
"""
import numpy as np
from numpy.random import random
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def gradient_descent(X, y, params, learning_rate, iterations):
m = len(y)
for i in range(iterations):
params = params - (learning_rate/m) * (X.T @ (sigmoid(X @ params) - y))
return (params)
def predict(X, params):
return np.round(sigmoid(X @ params))
x = np.atleast_2d(np.linspace(.25, .07, 50)).T
y = np.atleast_2d(np.concatenate((np.ones(25), np.zeros(25)))).T
m=len(y)
X = np.hstack((np.ones((m,1)),x))
n = np.size(X,1)
params = np.zeros((n,1))
iterations = 1500
learning_rate = 0.03
paramsOut = gradient_descent(X,y,params,learning_rate, iterations)
predict(X, paramsOut)
(1 / (1 + np.log(-(X[:,0]*params[0] + X[:,1]*params[1])) ))
params = params - (learning_rate/m) * X.T @ ((1 / (1 + np.log(-(X[:,0]*params[0] + X[:,1]*params[1])))) - y.T).T
y = np.reshape((correctRecord), (-1,1))
x = np.reshape((stimTimRec), (-1,1))
if len(x[:,0]) < len(x[0,:]):
x = x.T
if len(y[:,0]) < len(y[0,:]):
y = y.T
X = np.array([[ 1. , 0.2 ],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.19066667],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.18133333],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.172 ],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.16266667],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.15333333],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.144 ],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.13466667],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.12533333],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.116 ],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.10666667],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.09733333],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.088 ],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.07866667],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.06933333],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ],
[ 1. , 0.68365026],
[ 1. , -0.1 ],
[ 1. , 1. ]])
correctRecord = []
for ii in range(66):
if X[ii,1] == 1:
correctRecord.append(1)
elif X[ii,1] <0:
correctRecord.append(0)
else:
correctRecord.append(round(random()))
plt.scatter(X[:,1].T, correctRecord)
weights = sqrt(np.arange(1,len(correctRecord)+1, 1))
weights = weights / sum(weights)
weights = weights * len(correctRecord)
weights = np.atleast_2d(weights).T
y = np.atleast_2d(y).T
X = np.append(X, weights, 1)
X = np.append(X, y, 1)
params = np.array([0,0])
m = len(X[:,1])
iterations = 1500
learning_rate = .03
y = np.array(correctRecord)
for i in range(iterations):
params = params - (learning_rate/m) * (X.T @ ((1/(1+np.exp(-(X[:,0]*params[0] + X[:,1]*params[1]))))*X[:,2] - y))
predProbs = (1/(1+np.exp(-(X[:,0]*params[0] + X[:,1]*params[1]))))
plt.scatter(X[:,1], predProbs)
if sum(y[len(y)-10:len(y)])>=8:
curStimTim = curStimTim - .005;