njayaram2 commented on a change in pull request #356: Keras model arch table 
helper functions for keras_fit()
URL: https://github.com/apache/madlib/pull/356#discussion_r266088352
 
 

 ##########
 File path: src/ports/postgres/modules/convex/test/keras_model_arch_table.sql_in
 ##########
 @@ -0,0 +1,124 @@
+/* -----------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * ----------------------------------------------------------------------- */
+
+/* 
-----------------------------------------------------------------------------
+ * Test Keras Model Arch Table helper functions
+ * -------------------------------------------------------------------------- 
*/
+
+
+/* Test successful model creation where no table exists */
+DROP TABLE IF EXISTS test_keras_model_arch_table;
+SELECT keras_create_model('test_keras_model_arch_table', '{"a" : 1, "b" : 2, 
"c" : [4,5,6] }');
+
+SELECT assert(UPPER(atttypid::regtype::TEXT) = 'INTEGER', 'model_id column 
should be INTEGER type')
+    FROM pg_attribute WHERE attrelid = 'test_keras_model_arch_table'::regclass
+        AND attname = 'model_id';
+SELECT assert(UPPER(atttypid::regtype::TEXT) = 'JSON', 'model_arch column 
should be JSON type' ) FROM pg_attribute WHERE attrelid = 
'test_keras_model_arch_table'::regclass
+        AND attname = 'model_arch';
+SELECT assert(UPPER(atttypid::regtype::TEXT) =
+    'DOUBLE PRECISION[]', 'model_weights column should be DOUBLE PRECISION[] 
type')
+    FROM pg_attribute WHERE attrelid = 'test_keras_model_arch_table'::regclass
+        AND attname = 'model_weights';
+
+/*  model id should be 1 */
+SELECT assert(model_id = 1, 'Wrong model_id written by keras_create_model')
+    FROM test_keras_model_arch_table;
+
+/* model arch should be valid json, with all fields accessible with json 
operators */
+SELECT assert((model_arch->>'a') = '1', 'Cannot parse model_arch json in model 
table.')
+    FROM test_keras_model_arch_table;
+SELECT assert((model_arch->>'b') = '2', 'Cannot parse model_arch json in model 
table.')
+    FROM test_keras_model_arch_table;
+SELECT assert((model_arch->'c')->>0 = '4', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table;
+SELECT assert((model_arch->'c')->>1 = '5', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table;
+SELECT assert((model_arch->'c')->>2 = '6', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table;
+/* model_weights should be set to null, since this is not a warm start */
+SELECT assert(model_weights IS NULL, 'model_weights should be NULL after 
keras_create_model() called.') FROM test_keras_model_arch_table;
+
+
+/* Test model creation where valid table exists */
+SELECT keras_create_model('test_keras_model_arch_table', '{"config" : 
[1,2,3]}');
+SELECT keras_create_model('test_keras_model_arch_table', '{"config" : 
[8,4,0]}');
+SELECT assert(model_arch->'config'->>0 = '1', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 2;
+SELECT assert(model_arch->'config'->>1 = '2', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 2;
+SELECT assert(model_arch->'config'->>2 = '3', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 2;
+SELECT assert(model_arch->'config'->>0 = '8', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 3;
+SELECT assert(model_arch->'config'->>1 = '4', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 3;
+SELECT assert(model_arch->'config'->>2 = '0', 'Cannot parse model_arch json in 
model table.')
+    FROM test_keras_model_arch_table WHERE model_id = 3;
+
+/* Test deletion where valid table exists */
+SELECT keras_delete_model('test_keras_model_arch_table', 2);
+SELECT assert(COUNT(model_id) = 0, 'model id 2 should have been deleted!')
+    FROM test_keras_model_arch_table WHERE model_id = 2;
+SELECT keras_delete_model('test_keras_model_arch_table', 3);
+SELECT assert(COUNT(model_id) = 0, 'model id 3 should have been deleted!')
+    FROM test_keras_model_arch_table WHERE model_id = 3;
+      /* Delete a second time, to make sure nothing weird happens.
+       *  It should archrt to the user that the model_id wasn't found but not
+       *  raise an exception or change anything. */
+SELECT keras_delete_model('test_keras_model_arch_table', 3);
+SELECT assert(COUNT(model_id) = 0, 'model id 3 should have been deleted!')
+    FROM test_keras_model_arch_table WHERE model_id = 3;
+SELECT keras_delete_model('test_keras_model_arch_table', 1);
+SELECT assert(COUNT(relname) = 0, 'Table test_keras_model_arch_table should 
have been deleted.')
+    FROM pg_class where relname = 'test_keras_model_arch_table';
+
+SELECT keras_create_model('test_keras_model_arch_table', '{"config" : 
[1,2,3]}');
+DELETE FROM test_keras_model_arch_table;
+
+/* Test deletion where empty table exists */
+SELECT keras_delete_model('test_keras_model_arch_table', 3);
+SELECT assert(COUNT(relname) = 0, 'Table test_keras_model_arch_table should 
have been deleted.') from pg_class where relname = 
'test_keras_model_arch_table';
+
+/* Test deletion where invalid table exists */
+
+SELECT keras_create_model('test_keras_model_arch_table', '{"config" : 
[1,2,3]}');
+ALTER TABLE test_keras_model_arch_table DROP COLUMN model_id;
+CREATE FUNCTION trap_error(stmt TEXT) RETURNS INTEGER AS $$
+BEGIN
+    BEGIN
+        EXECUTE stmt;
+       EXCEPTION
+        WHEN OTHERS THEN
+            RETURN 1;
+    END;
+    RETURN 0;
+END;
+$$ LANGUAGE plpgsql;
 
 Review comment:
   +1
   I think we should create this as a madlib function so that it gets created 
when we install madlib. This can be very useful in other dev checks as well.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to