Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Learning scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Mar 23, 2012
1 parent 98f5d7d commit 542ae45
Show file tree
Hide file tree
Showing 42 changed files with 6,127 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CryDetector/src/cc/wthr/crydetector/CryDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void link(int sessionId) {
throw new RuntimeException(e);
}
}

mEqualizer = new Equalizer(0, sessionId);
mEqualizer.setEnabled(true);

Expand All @@ -81,7 +81,7 @@ public void link(int sessionId) {
mVisualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), false, true);
mVisualizer.setEnabled(true);
}

public void unlink() {
if(mVisualizer != null) {
mVisualizer.setEnabled(false);
Expand Down
2 changes: 1 addition & 1 deletion CryDetector/src/cc/wthr/crydetector/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void onClick(View view) {
try {
mPlayer = MediaPlayer.create(this, soundId);
mCryDetector.link(mPlayer.getAudioSessionId());
mPlayer.setOnCompletionListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.start();
} catch(Throwable e) {
Log.d("MainActivity", "Link visualizer error", e);
Expand Down
16 changes: 16 additions & 0 deletions CryTeacher/logs/cry1.txt

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions CryTeacher/logs/cry10.txt

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions CryTeacher/logs/cry11.txt

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions CryTeacher/logs/cry12.txt

Large diffs are not rendered by default.

563 changes: 563 additions & 0 deletions CryTeacher/logs/cry15.txt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions CryTeacher/logs/cry2.txt

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions CryTeacher/logs/cry3.txt

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions CryTeacher/logs/cry5.txt

Large diffs are not rendered by default.

134 changes: 134 additions & 0 deletions CryTeacher/logs/cry6.txt

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions CryTeacher/logs/cry7.txt

Large diffs are not rendered by default.

102 changes: 102 additions & 0 deletions CryTeacher/logs/cry9.txt

Large diffs are not rendered by default.

588 changes: 588 additions & 0 deletions CryTeacher/logs/nocry1.txt

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions CryTeacher/logs/nocry10.txt

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions CryTeacher/logs/nocry11.txt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions CryTeacher/logs/nocry12.txt

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions CryTeacher/logs/nocry13.txt

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions CryTeacher/logs/nocry14.txt

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions CryTeacher/logs/nocry15.txt

Large diffs are not rendered by default.

436 changes: 436 additions & 0 deletions CryTeacher/logs/nocry17.txt

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions CryTeacher/logs/nocry3.txt

Large diffs are not rendered by default.

201 changes: 201 additions & 0 deletions CryTeacher/logs/nocry4.txt

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions CryTeacher/logs/nocry5.txt

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions CryTeacher/logs/nocry6.txt

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions CryTeacher/logs/nocry7.txt

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions CryTeacher/logs/nocry8.txt

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions CryTeacher/logs/nocry9.txt

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions CryTeacher/matlab/costFunction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%


J = 1/m * ( -y' * log(sigmoid(X*theta)) - (1-y)' * log(1 - sigmoid(X*theta)));

grad = X' * (sigmoid(X*theta)-y) ./ m;




% =============================================================

end
30 changes: 30 additions & 0 deletions CryTeacher/matlab/costFunctionReg.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
% theta as the parameter for regularized logistic regression and the
% gradient of the cost w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta

% do not regularize on the Theta0
theta_1 = theta;
theta_1(1) = 0;

J = 1/m * ( -y' * log(sigmoid(X*theta)) - (1-y)' * log(1 - sigmoid(X*theta))) + lambda/(2*m) * sum (theta_1 .* theta_1);

grad = (X' * (sigmoid(X*theta)-y) + lambda .* theta_1)./ m;

% =============================================================

end
2,838 changes: 2,838 additions & 0 deletions CryTeacher/matlab/data.txt

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions CryTeacher/matlab/featureNormalize.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X
% FEATURENORMALIZE(X) returns a normalized version of X where
% the mean value of each feature is 0 and the standard deviation
% is 1. This is often a good preprocessing step to do when
% working with learning algorithms.

mu = mean(X);
X_norm = bsxfun(@minus, X, mu);

sigma = std(X_norm);
X_norm = bsxfun(@rdivide, X_norm, sigma);


% ============================================================

end
25 changes: 25 additions & 0 deletions CryTeacher/matlab/predict.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic
%regression parameters theta
% p = PREDICT(theta, X) computes the predictions for X using a
% threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned logistic regression parameters.
% You should set p to a vector of 0's and 1's
%

p = sigmoid(X*theta) >= 0.5;



% =========================================================================


end
18 changes: 18 additions & 0 deletions CryTeacher/matlab/sigmoid.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.

% You need to return the following variables correctly
g = zeros(size(z));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).


g = 1 ./ (1 + exp(-1 .* z));


% =============================================================

end
98 changes: 98 additions & 0 deletions CryTeacher/matlab/train.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
% Cry Detector Training

data = load('data.txt');

% Split data to train, test and crossvalidation sets
total_count = size(data,1);
data = data(randperm(total_count), :);

train_count = int32(total_count*0.6);
test_count = int32(total_count*0.2);
cv_count = total_count - train_count - test_count;


Xtrain = data(1:train_count, 1:128);
ytrain = data(1:train_count, 129);

Xtest = data(train_count+1:train_count+test_count, 1:128);
ytest = data(train_count+1:train_count+test_count, 129);
rtest = data(train_count+1:train_count+test_count, 130);

Xval = data(train_count+test_count+1:total_count, 1:128);
yval = data(train_count+test_count+1:total_count, 129);

X = Xtrain;
y = ytrain;

% Xval = featureNormalize(Xval);
% yval = featureNormalize(yval);

#lambda_vec = [0, 0.1, 0.5, 1, 10, 50, 100 1000 5000 10000 30000 50000 60000 100000]';
lambda_vec = [1000 5000 10000 30000]';
#error_train = zeros(length(lambda_vec), 1);
#error_val = zeros(length(lambda_vec), 1);

[m, n] = size(X);
X = [ones(m, 1) X]; % Add intercept term to X
Xval = [ones(cv_count, 1) Xval]; % Add intercept term to Xval
Xtest = [ones(test_count, 1) Xtest];

% Compute and display initial cost and gradient
initial_theta = zeros(n + 1, 1); % Initialize fitting parameters
[cost, grad] = costFunctionReg(initial_theta, X, y, 0);
fprintf('Cost at initial theta (zeros): %f\n', cost);

options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunctionReg(t, X, y, 0)), initial_theta, options);

% error_train = zeros(size(1:50:m,2), 1);
% error_val = zeros(size(1:50:m,2), 1);
% for k=1:50:m
% fprintf('%d\n', k);
% Xtrain = X(1:k,:);
% ytrain = y(1:k);
% [theta, cost] = fminunc(@(t)(costFunctionReg(t, Xtrain, ytrain, 0)), initial_theta, options);
% error_train(int32(k/50)+1) = 100 * (1 - mean(predict(theta, Xtrain) == ytrain));
% error_val(int32(k/50)+1) = 100 * (1 - mean(predict(theta, Xval) == yval));
%end

% plot(1:50:m, error_train, 1:50:m, error_val);
% title('Learning curve for linear regression')
% legend('Train', 'Cross Validation')
% xlabel('Number of training examples')
% ylabel('Error')

% Selecting right lambda
% options = optimset('GradObj', 'on', 'MaxIter', 10000);
% for k = 1:length(lambda_vec)
% lambda = lambda_vec(k);
% [theta, cost] = fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);
% error_train(k) = 100 * (1 - mean(predict(theta, X) == y));
% error_val(k) = 100 * (1 - mean(predict(theta, Xval) == yval));
% error_train(k)
% error_val(k)
% fprintf('%f Cost at theta found by fminunc: %f\n', lambda, cost);
%end

% plot(lambda_vec, error_train, lambda_vec, error_val);
% title('Learning curve for linear regression')
% legend('Train', 'Cross Validation')
% xlabel('lambda')
% ylabel('Error')


% Compute accuracy on our training set
p = predict(theta, Xtest);

fprintf('Train Accuracy: %f \n', mean(double(p == ytest)) * 100);
fprintf('Total size %d \n', length(rtest == 1));
fprintf('Total right predicted %d \n', sum(predict(theta, Xtest) == ytest));
fprintf('Right predicted cries: %f \n', sum((predict(theta, Xtest) == ytest)(ytest==1)));
fprintf('Right predicted nocries: %f \n', sum((predict(theta, Xtest) == ytest)(ytest==0)));
fprintf('Not predicted cries: %f \n', sum((predict(theta, Xtest) != ytest)(ytest==1)));
fprintf('Wrong predicted cries: %f \n', sum((predict(theta, Xtest) != ytest)(ytest==0)));
fprintf('Total Cry %d\n', sum(ytest));
fprintf('Total NoCry %d\n', sum(ytest==0));
pause

theta
Loading

0 comments on commit 542ae45

Please sign in to comment.