Advertisement
Guest User

Untitled

a guest
May 31st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1.  
  2. import find_mxnet
  3. import mxnet as mx
  4. from load_data import load
  5. from sklearn.cross_validation import train_test_split
  6. import logging
  7. import pdb as pdb
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10.  
  11. def get_mlp():
  12. """
  13. multi-layer perceptron
  14. """
  15.  
  16. outLabl = mx.sym.Variable('softmax_label')
  17.  
  18. data = mx.symbol.Variable('data')
  19.  
  20. flat = mx.symbol.Flatten(data=data)
  21.  
  22. fc1 = mx.symbol.FullyConnected(data = flat, name='fc1', num_hidden=100)
  23. act1 = mx.symbol.Activation(data = fc1, name='relu1', act_type="relu")
  24. fc2 = mx.symbol.FullyConnected(data = act1, name='fc2', num_hidden=30)
  25. net = mx.sym.LinearRegressionOutput(data=fc2, label=outLabl, name='linreg1')
  26.  
  27. return net
  28.  
  29. #
  30. # Load data
  31. #
  32. # Create artificial data
  33. X = np.ones((2140, 9216)).reshape((2140, 1, 96, 96))
  34. y = 0.6*np.ones((2140, 30))
  35.  
  36. #
  37. # Setup iterators
  38. #
  39. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state = 42)
  40. trainIter = mx.io.NDArrayIter(data = X_train, label = y_train, batch_size = 64)
  41. valIter = mx.io.NDArrayIter(data = X_test , label = y_test , batch_size = 64)
  42.  
  43. #
  44. # Multidevice kvstore setup and logging
  45. #
  46. kv = mx.kvstore.create('local')
  47. head = '%(asctime)-15s Node[' + str(kv.rank) + '] %(message)s'
  48. logging.basicConfig(level=logging.DEBUG, format=head)
  49.  
  50. #
  51. # Get model and train
  52. #
  53. net = get_mlp()
  54.  
  55. model = mx.model.FeedForward(
  56. ctx = mx.gpu(),
  57. symbol = net,
  58. num_epoch = 15,
  59. learning_rate = 0.001,
  60. momentum = 0.9,
  61. wd = 0.00001,
  62. initializer = mx.init.Xavier(factor_type="in", magnitude=2.34),
  63. )
  64. model.fit(X=trainIter, eval_data=valIter, batch_end_callback=mx.callback.Speedometer(1,50), epoch_end_callback=None, eval_metric='rmse')
  65.  
  66. #
  67. # Prediction
  68. #
  69.  
  70. valIter.reset()
  71. for prediction in model.predict(valIter):
  72. print prediction
  73. pdb.set_trace()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement