Skip to content

Instantly share code, notes, and snippets.

@Ye-Tian-Zero
Created April 18, 2016 01:47
Show Gist options
  • Save Ye-Tian-Zero/4947c5940fa32bf1d029f4f7feeb0876 to your computer and use it in GitHub Desktop.
Save Ye-Tian-Zero/4947c5940fa32bf1d029f4f7feeb0876 to your computer and use it in GitHub Desktop.
deeplearning study
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="projectConfiguration" value="Nosetests" />
<option name="PROJECT_TEST_RUNNER" value="Nosetests" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/LogisticRegression.py" charset="GBK" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 2.7.11 (D:\Anaconda2\python.exe)" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/DeepLearning.iml" filepath="$PROJECT_DIR$/.idea/DeepLearning.iml" />
</modules>
</component>
</project>
import theano
import theano.tensor as T
from theano import function
import numpy as np
import matplotlib.pyplot as plt
class LR(object):
x = T.dmatrix('x')
y = T.lvector('y')
def __init__(self, i_dim, max_train_step):
self.train_step = max_train_step
self.rng = np.random
self.w = theano.shared(self.rng.randn(i_dim), name = 'w')
self.b = theano.shared(0.0, name = 'b')
self.logisticFunc = 1 / (1 + T.exp(-T.dot(self.x, self.w) - self.b))
self.prediction = self.logisticFunc > 0.5
self.cross_entropy = -self.y * T.log(self.logisticFunc) - (1 - self.y) * T.log(1 - self.logisticFunc)
self.cost = self.cross_entropy.mean() + 0.01 * (self.w ** 2).sum()
self.gw, self.gb = T.grad(self.cost, [self.w, self.b])
self.trainFunc = function([self.x, self.y], [self.prediction, self.cost],
updates = ((self.w, self.w - 0.1 * self.gw), (self.b, self.b - 0.1 * self.gb)))
self.predict = function([self.x], self.prediction)
def train(self, data):
for i in range(self.train_step):
print i
prediction, cost = self.trainFunc(data[0], data[1])
def predict(self, data):
return self.predict(data)
if __name__ == '__main__':
N = 400
feats = 784
D = [np.random.randn(N, feats), np.random.randint(0,2,N)]
lr = LR(784, 10000)
lr.train(D)
print lr.predict(D[0])
print D[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment