|
52 | 52 | type(layer_t) hidden_layers
|
53 | 53 | type(neuron_t) output_neuron
|
54 | 54 | real(rkind), allocatable :: hidden_weights(:,:,:)
|
| 55 | + integer l |
| 56 | + character(len=:), allocatable :: quoted_value, line |
55 | 57 |
|
56 | 58 | lines = file_%lines()
|
57 | 59 |
|
58 |
| - call assert(adjustl(lines(1)%string())=="{", "from_json: expecting '{' for to start outermost object", lines(1)%string()) |
59 |
| - call assert(adjustl(lines(2)%string())=='"hidden_layers": [', 'from_json: expecting "hidden_layers": [', lines(2)%string()) |
| 60 | + l = 1 |
| 61 | + call assert(adjustl(lines(l)%string())=="{", "construct_from_json: expecting '{' to start outermost object", lines(l)%string()) |
| 62 | + l = 2 |
| 63 | + if (adjustl(lines(l)%string()) /= '"metadata": {') then |
| 64 | + inference_engine%metadata_ = metadata_t(modelName="",modelAuthor="",compilationDate="", usingSkipConnections=.false.) |
| 65 | + else |
| 66 | + l = l + 1 |
| 67 | + inference_engine%metadata_%modelName = get_string_value(adjustl(lines(l)%string()), key="modelName") |
| 68 | + |
| 69 | + l = l + 1 |
| 70 | + inference_engine%metadata_%modelAuthor = get_string_value(adjustl(lines(l)%string()), key="modelAuthor") |
| 71 | + |
| 72 | + l = l + 1 |
| 73 | + inference_engine%metadata_%compilationDate = get_string_value(adjustl(lines(l)%string()), key="compilationDate") |
| 74 | + |
| 75 | + l = l + 1 |
| 76 | + inference_engine%metadata_%usingSkipConnections = get_logical_value(adjustl(lines(l)%string()), key="usingSkipConnections") |
| 77 | + |
| 78 | + l = l + 1 |
| 79 | + call assert(adjustl(lines(l)%string())=="},", "construct_from_json: expecting '},' to end metadata object", lines(l)%string()) |
| 80 | + |
| 81 | + l = l + 1 |
| 82 | + end if |
| 83 | + |
| 84 | + call assert(adjustl(lines(l)%string())=='"hidden_layers": [', 'from_json: expecting "hidden_layers": [', lines(l)%string()) |
| 85 | + l = l + 1 |
60 | 86 |
|
61 | 87 | block
|
62 |
| - integer, parameter :: first_layer_line=3, lines_per_neuron=4, bracket_lines_per_layer=2 |
| 88 | + integer, parameter :: lines_per_neuron=4, bracket_lines_per_layer=2 |
63 | 89 | character(len=:), allocatable :: output_layer_line
|
64 | 90 |
|
65 |
| - hidden_layers = layer_t(lines, start=first_layer_line) |
| 91 | + hidden_layers = layer_t(lines, start=l) |
66 | 92 |
|
67 |
| - associate( output_layer_line_number => first_layer_line + lines_per_neuron*sum(hidden_layers%count_neurons()) & |
| 93 | + associate( output_layer_line_number => l + lines_per_neuron*sum(hidden_layers%count_neurons()) & |
68 | 94 | + bracket_lines_per_layer*hidden_layers%count_layers() + 1)
|
69 | 95 |
|
70 | 96 | output_layer_line = lines(output_layer_line_number)%string()
|
|
113 | 139 |
|
114 | 140 | call assert_consistent(inference_engine)
|
115 | 141 |
|
| 142 | + contains |
| 143 | + |
| 144 | + pure function get_string_value(line, key) result(value_) |
| 145 | + character(len=*), intent(in) :: line, key |
| 146 | + character(len=:), allocatable :: value_ |
| 147 | + |
| 148 | + associate(opening_key_quotes => index(line, '"'), separator => index(line, ':')) |
| 149 | + associate(closing_key_quotes => opening_key_quotes + index(line(opening_key_quotes+1:), '"')) |
| 150 | + associate(unquoted_key => line(opening_key_quotes+1:closing_key_quotes-1), remainder => line(separator+1:)) |
| 151 | + call assert(unquoted_key == key,"construct_from_json(get_string_value): unquoted_key == key ", unquoted_key) |
| 152 | + associate(opening_value_quotes => index(remainder, '"')) |
| 153 | + associate(closing_value_quotes => opening_value_quotes + index(remainder(opening_value_quotes+1:), '"')) |
| 154 | + value_ = remainder(opening_value_quotes+1:closing_value_quotes-1) |
| 155 | + end associate |
| 156 | + end associate |
| 157 | + end associate |
| 158 | + end associate |
| 159 | + end associate |
| 160 | + end function |
| 161 | + |
| 162 | + pure function get_logical_value(line, key) result(value_) |
| 163 | + character(len=*), intent(in) :: line, key |
| 164 | + logical value_ |
| 165 | + character(len=:), allocatable :: remainder ! a gfortran bug prevents making this an association |
| 166 | + |
| 167 | + associate(opening_key_quotes => index(line, '"'), separator => index(line, ':')) |
| 168 | + associate(closing_key_quotes => opening_key_quotes + index(line(opening_key_quotes+1:), '"')) |
| 169 | + associate(unquoted_key => line(opening_key_quotes+1:closing_key_quotes-1)) |
| 170 | + call assert(unquoted_key == key,"construct_from_json(get_string_value): unquoted_key == key ", unquoted_key) |
| 171 | + remainder = adjustl(line(separator+1:)) |
| 172 | + call assert(any(remainder == ["true ", "false"]), "construct_from_json(get_logical_value): valid value", remainder) |
| 173 | + value_ = remainder == "true" |
| 174 | + end associate |
| 175 | + end associate |
| 176 | + end associate |
| 177 | + end function |
| 178 | + |
116 | 179 | end procedure construct_from_json
|
117 | 180 |
|
118 | 181 |
|
|
0 commit comments