How to use test_env_spec method in tox

Best Python code snippet using tox_python

test_gaussian_cnn_baseline.py

Source:test_gaussian_cnn_baseline.py Github

copy

Full Screen

1import pickle2from unittest import mock3import akro4import numpy as np5import pytest6import tensorflow as tf7from garage.envs.env_spec import EnvSpec8from garage.tf.baselines import GaussianCNNBaseline9from garage.tf.optimizers import LbfgsOptimizer10from tests.fixtures import TfGraphTestCase11def get_train_test_data():12 matrices = [13 np.linspace(i - 0.5, i + 0.5, 300).reshape((10, 10, 3))14 for i in range(110)15 ]16 data = [np.sin(matrices[i]) for i in range(100)]17 obs = [{'observations': [x], 'returns': [np.mean(x)]} for x in data]18 observations = np.concatenate([p['observations'] for p in obs])19 returns = np.concatenate([p['returns'] for p in obs])20 returns = returns.reshape((-1, 1))21 paths = {'observations': [np.sin(matrices[i]) for i in range(100, 110)]}22 expected = [[np.mean(x)] for x in paths['observations']]23 return (obs, observations, returns), (paths, expected)24test_env_spec = EnvSpec(observation_space=akro.Box(low=-1,25 high=1,26 shape=(10, 10, 3)),27 action_space=None)28class TestGaussianCNNBaseline(TfGraphTestCase):29 @pytest.mark.large30 def test_fit_normalized(self):31 gcr = GaussianCNNBaseline(env_spec=test_env_spec,32 filters=((3, (3, 3)), (6, (3, 3))),33 strides=(1, 1),34 padding='SAME',35 hidden_sizes=(32, ),36 adaptive_std=False,37 use_trust_region=True)38 train_data, test_data = get_train_test_data()39 train_paths, observations, returns = train_data40 for _ in range(20):41 gcr.fit(train_paths)42 test_paths, expected = test_data43 prediction = gcr.predict(test_paths)44 average_error = 0.045 for i, exp in enumerate(expected):46 average_error += np.abs(exp - prediction[i])47 average_error /= len(expected)48 assert average_error <= 0.149 x_mean = self.sess.run(gcr._networks['default'].x_mean)50 x_mean_expected = np.mean(observations, axis=0, keepdims=True)51 x_std = self.sess.run(gcr._networks['default'].x_std)52 x_std_expected = np.std(observations, axis=0, keepdims=True)53 assert np.allclose(x_mean, x_mean_expected)54 assert np.allclose(x_std, x_std_expected)55 y_mean = self.sess.run(gcr._networks['default'].y_mean)56 y_mean_expected = np.mean(returns, axis=0, keepdims=True)57 y_std = self.sess.run(gcr._networks['default'].y_std)58 y_std_expected = np.std(returns, axis=0, keepdims=True)59 assert np.allclose(y_mean, y_mean_expected)60 assert np.allclose(y_std, y_std_expected)61 @pytest.mark.large62 def test_fit_unnormalized(self):63 gcr = GaussianCNNBaseline(env_spec=test_env_spec,64 filters=((3, (3, 3)), (6, (3, 3))),65 strides=(1, 1),66 padding='SAME',67 hidden_sizes=(32, ),68 adaptive_std=True,69 normalize_inputs=False,70 normalize_outputs=False)71 train_data, test_data = get_train_test_data()72 train_paths, _, _ = train_data73 for _ in range(20):74 gcr.fit(train_paths)75 test_paths, expected = test_data76 prediction = gcr.predict(test_paths)77 average_error = 0.078 for i, exp in enumerate(expected):79 average_error += np.abs(exp - prediction[i])80 average_error /= len(expected)81 assert average_error <= 0.182 x_mean = self.sess.run(gcr._networks['default'].x_mean)83 x_mean_expected = np.zeros_like(x_mean)84 x_std = self.sess.run(gcr._networks['default'].x_std)85 x_std_expected = np.ones_like(x_std)86 assert np.array_equal(x_mean, x_mean_expected)87 assert np.array_equal(x_std, x_std_expected)88 y_mean = self.sess.run(gcr._networks['default'].y_mean)89 y_mean_expected = np.zeros_like(y_mean)90 y_std = self.sess.run(gcr._networks['default'].y_std)91 y_std_expected = np.ones_like(y_std)92 assert np.allclose(y_mean, y_mean_expected)93 assert np.allclose(y_std, y_std_expected)94 @pytest.mark.large95 def test_fit_smaller_subsample_factor(self):96 gcr = GaussianCNNBaseline(env_spec=test_env_spec,97 filters=((3, (3, 3)), (6, (3, 3))),98 strides=(1, 1),99 padding='SAME',100 hidden_sizes=(32, ),101 subsample_factor=0.9,102 adaptive_std=False)103 train_data, test_data = get_train_test_data()104 train_paths, _, _ = train_data105 for _ in range(20):106 gcr.fit(train_paths)107 test_paths, expected = test_data108 prediction = gcr.predict(test_paths)109 average_error = 0.0110 for i, exp in enumerate(expected):111 average_error += np.abs(exp - prediction[i])112 average_error /= len(expected)113 assert average_error <= 0.1114 @pytest.mark.large115 def test_fit_without_trusted_region(self):116 gcr = GaussianCNNBaseline(env_spec=test_env_spec,117 filters=((3, (3, 3)), (6, (3, 3))),118 strides=(1, 1),119 padding='SAME',120 hidden_sizes=(32, ),121 adaptive_std=False,122 use_trust_region=False)123 train_data, test_data = get_train_test_data()124 train_paths, _, _ = train_data125 for _ in range(20):126 gcr.fit(train_paths)127 test_paths, expected = test_data128 prediction = gcr.predict(test_paths)129 average_error = 0.0130 for i, exp in enumerate(expected):131 average_error += np.abs(exp - prediction[i])132 average_error /= len(expected)133 assert average_error <= 0.1134 @mock.patch('tests.garage.tf.baselines.'135 'test_gaussian_cnn_baseline.'136 'LbfgsOptimizer')137 def test_optimizer_args(self, mock_lbfgs):138 lbfgs_args = dict(max_opt_itr=25)139 gcr = GaussianCNNBaseline(env_spec=test_env_spec,140 filters=((3, (3, 3)), (6, (3, 3))),141 strides=(1, 1),142 padding='SAME',143 hidden_sizes=(32, ),144 optimizer=LbfgsOptimizer,145 optimizer_args=lbfgs_args,146 use_trust_region=True)147 assert mock_lbfgs.return_value is gcr._optimizer148 mock_lbfgs.assert_called_with(max_opt_itr=25)149 def test_is_pickleable(self):150 gcr = GaussianCNNBaseline(env_spec=test_env_spec,151 filters=((3, (3, 3)), (6, (3, 3))),152 strides=(1, 1),153 padding='SAME',154 hidden_sizes=(32, ),155 adaptive_std=False,156 use_trust_region=False)157 with tf.compat.v1.variable_scope('GaussianCNNBaseline', reuse=True):158 bias = tf.compat.v1.get_variable(159 'dist_params/mean_network/hidden_0/bias')160 bias.load(tf.ones_like(bias).eval())161 _, test_data = get_train_test_data()162 test_paths, _ = test_data163 result1 = gcr.predict(test_paths)164 h = pickle.dumps(gcr)165 with tf.compat.v1.Session(graph=tf.Graph()):166 gcr_pickled = pickle.loads(h)167 result2 = gcr_pickled.predict(test_paths)168 assert np.array_equal(result1, result2)169 def test_param_values(self):170 gcb = GaussianCNNBaseline(env_spec=test_env_spec,171 filters=((3, (3, 3)), (6, (3, 3))),172 strides=(1, 1),173 padding='SAME',174 hidden_sizes=(32, ),175 adaptive_std=False,176 use_trust_region=False)177 new_gcb = GaussianCNNBaseline(env_spec=test_env_spec,178 filters=((3, (3, 3)), (6, (3, 3))),179 strides=(1, 1),180 padding='SAME',181 hidden_sizes=(32, ),182 adaptive_std=False,183 use_trust_region=False,184 name='GaussianCNNBaseline2')185 # Manual change the parameter of GaussianCNNBaseline186 with tf.compat.v1.variable_scope('GaussianCNNBaseline', reuse=True):187 bias_var = tf.compat.v1.get_variable(188 'dist_params/mean_network/hidden_0/bias')189 bias_var.load(tf.ones_like(bias_var).eval())190 old_param_values = gcb.get_param_values()191 new_param_values = new_gcb.get_param_values()192 assert not np.array_equal(old_param_values, new_param_values)193 new_gcb.set_param_values(old_param_values)194 new_param_values = new_gcb.get_param_values()195 assert np.array_equal(old_param_values, new_param_values)196 def test_get_params(self):197 gcb = GaussianCNNBaseline(env_spec=test_env_spec,198 filters=((3, (3, 3)), (6, (3, 3))),199 strides=(1, 1),200 padding='SAME',201 hidden_sizes=(32, ),202 adaptive_std=False,203 use_trust_region=False)204 params_interal = gcb.get_params()205 trainable_params = tf.compat.v1.trainable_variables(206 scope='GaussianCNNBaseline')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful