<jupyter_start><jupyter_text>WBAI Aphasia (Pure Alexia) Handson 00    17/Sep/2018浅川 伸一 &lt;<EMAIL>&gt;-->    浅川 伸一 &lt;<EMAIL>&gt;<jupyter_code># -*-: coding utf-8 -*-
import sys
import numpy as np
import codecs
from sklearn.neural_network import MLPClassifier
import matplotlib.pyplot as plt
%matplotlib inline
### preparation to draw graphs
plt.rcParams['figure.figsize'] = (12, 8) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

np.set_printoptions(precision=3, suppress=True)
import wbai_aphasia as handson
from wbai_aphasia import tanh, sigmoid, relu
from wbai_aphasia import xavier_initializer
# Here the enviroment that I am computing.
!date; printf '\n'
!uname -a; printf '\n'
!type python; printf '\n'
!python --version; printf '\n'
!gcc --version; printf '\n'
!conda --version; printf '\n'
original_file='../data/PMSP96.orig'
with codecs.open(original_file,'r') as f:
    lines = f.readlines()<jupyter_output><empty_output><jupyter_text>---Plaut et. al. (1996) Fig. 1- <NAME>., <NAME>., <NAME>., & <NAME>. (1996). Understanding normal and impaired word reading: Computational principles in quasi-regular domains. _Psychological Review_, 103, 56-115.We will refer to the above as PMSP96 henceforth.--- ---PMSP96 Table 2---<jupyter_code>filename = '../data/PMSP96.orig'
x = codecs.open(filename,'r','utf-8').readlines()
x[:4]
inp, inpStr, out, outStr, freq = list(), list(), list(), list(), list()
wrd_class = {}  # dict
for i, line in enumerate(lines):
    x = lines[i].strip().split()
    if i % 3 == 0:
        inpStr.append(x[2])
        outStr.append(x[3])
        freq.append(x[4])
        #if x[5] in wrd_class:
        #    wrd_class[x[5]] += 1
        #else:
        #    wrd_class[x[5]] = 1
        wrd_class[x[5]] = wrd_class[x[5]] + 1 if x[5] in wrd_class else 1
    elif i % 3 == 1:
        inp.append(np.array(x,dtype=np.int32))
    else:
        out.append(np.array(x,dtype=np.int32))

X = np.array(inp)
y = np.array(out)
#n = 0
#for k in wrd_class:
#    n += wrd_class[k]
n = 0
for k in sorted(wrd_class):
    n += wrd_class[k] if k is not '#' else 0
    print('{0:7s} {1:03d}'.format(k, wrd_class[k]))
print('---\nTotal: {} except for #'.format(n))<jupyter_output><empty_output><jupyter_text>---PMS96 Appendix A---                                                       --- Today task that we must tackle with is belowPMSP96 Table 7--- Glushko (1979) Appendix Table 1- <NAME>. (1979). The organization and activation of orthographic knowledge in reading aloud. _Journal of Experimental Psyhology: Human Perception and Performance_, 5, 674-691.---<jupyter_code>nKfold = 5
random_state = 2
perms = np.random.RandomState(random_state).permutation(len(X)) % nKfold
hidden_layers = (128, 128)
params = [{'hidden_layer_sizes': hidden_layers, 'solver': 'sgd', 'learning_rate': 'constant', 
           'momentum': 0.5, 'nesterovs_momentum': False, 'learning_rate_init': 0.1, 
           'activation': 'relu'},
          {'hidden_layer_sizes': hidden_layers, 'solver': 'sgd', 'learning_rate': 'constant', 
           'momentum': 0.5, 'nesterovs_momentum': False, 'learning_rate_init': 0.1, 'activation': 
           'tanh'},
          {'hidden_layer_sizes': hidden_layers, 'solver': 'adam', 'learning_rate_init': 0.01, 
           'activation': 'relu'},
          {'hidden_layer_sizes': hidden_layers, 'solver': 'adam', 'learning_rate_init': 0.01, 
           'activation': 'tanh'}
         ]

labels = [ 'SGD, relu', 'SGD, tanh', 'Adam, relu', 'Adam, tanh']

plot_args = [{'c': 'red', 'linestyle': '-', 'label': 'SGD, relu', 'linewidth': 1},
             {'c': 'green', 'linestyle': '--', 'label': 'SGD, tanh', 'linewidth': 3},
             {'c': 'blue', 'linestyle': '-', 'label': 'Adam, relu', 'linewidth': 1},
             {'c': 'black', 'linestyle': '--', 'label' : 'Adam, tanh', 'linewidth': 3}]
mlps = []
for label, param, plot_arg in zip(labels, params, plot_args):
    print('-' * 16)
    print('training: {}'.format(label))
    print('-' * 16)
    
    mlp = MLPClassifier(max_iter=200, 
                        alpha=1e-4,
                        verbose=False, 
                        tol=1e-4, 
                        random_state=0, 
                        early_stopping=False,
                        **param)
    
    for i in range(nKfold):
        X_train, y_train = X[perms != i], y[perms != i]
        X_test, y_test = X[perms == i], y[perms == i]
        mlp.fit(X_train, y_train)
        plt.plot(mlp.loss_curve_, **plot_arg)
        print("Training accuracy: {:.3f}".format(mlp.score(X_train, y_train)), end=' ')
        print("Test accuracy: {:.3f}".format(mlp.score(X_test,y_test)))
    plt.legend(loc='upper right')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1722397476340694, 'fraction_numerical': 0.02754994742376446, 'mean_word_length': 3.0202874049027897, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '488140', 'license': 'Apache-2.0', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1699, 'n_words': 414, 'path': 'examples/2018wbai_aphasia_handson_baseline.ipynb', 'repo_name': 'ShinAsakawa/wbai_aphasia', 'size': '4808'}