(self, key, feed_dict, batch)
| 397 | return req |
| 398 | |
| 399 | def process_tensor(self, key, feed_dict, batch): |
| 400 | lod = [] |
| 401 | if "{}.lod".format(key) in feed_dict: |
| 402 | lod = feed_dict["{}.lod".format(key)] |
| 403 | shape = self.feed_shapes_[key].copy() |
| 404 | elem_type = self.feed_types_[key] |
| 405 | data_value = feed_dict[key] |
| 406 | data_key = proto_data_key_list[elem_type] |
| 407 | proto_index = self.feed_names_to_idx_[key] |
| 408 | name = self.feed_real_names[proto_index] |
| 409 | alias_name = key |
| 410 | |
| 411 | # feed_dict[key] 可以是np.ndarray |
| 412 | # 也可以是list或tuple |
| 413 | # 当np.ndarray需要处理为list |
| 414 | if isinstance(feed_dict[key], np.ndarray): |
| 415 | shape_lst = [] |
| 416 | # 0维numpy 需要在外层再加一个[] |
| 417 | if feed_dict[key].ndim == 0: |
| 418 | data_value = [feed_dict[key].tolist()] |
| 419 | shape_lst.append(1) |
| 420 | else: |
| 421 | shape_lst.extend(list(feed_dict[key].shape)) |
| 422 | shape = shape_lst |
| 423 | data_value = feed_dict[key].flatten().tolist() |
| 424 | # 当Batch为False,shape字段前插一个1,表示batch维 |
| 425 | # 当Batch为True,则直接使用numpy.shape作为batch维度 |
| 426 | if batch == False: |
| 427 | shape.insert(0, 1) |
| 428 | |
| 429 | # 当是list或tuple时,需要把多层嵌套展开 |
| 430 | elif isinstance(feed_dict[key], (list, tuple)): |
| 431 | # 当Batch为False,shape字段前插一个1,表示batch维 |
| 432 | # 当Batch为True, 由于list并不像numpy那样规整,所以 |
| 433 | # 无法获取shape,此时取第一维度作为Batch维度. |
| 434 | # 插入到feedVar.shape前面. |
| 435 | if batch == False: |
| 436 | shape.insert(0, 1) |
| 437 | else: |
| 438 | shape.insert(0, len(feed_dict[key])) |
| 439 | feed_dict[key] = [x for x in list_flatten(feed_dict[key])] |
| 440 | data_value = feed_dict[key] |
| 441 | else: |
| 442 | # 输入可能是单个的str或int值等 |
| 443 | # 此时先统一处理为一个list |
| 444 | # 由于输入比较特殊,shape保持原feedvar中不变 |
| 445 | data_value = [] |
| 446 | if isinstance(feed_dict[key], (str, bytes)): |
| 447 | if self.feed_types_[key] != bytes_type: |
| 448 | raise ValueError( |
| 449 | "feedvar is not string-type,feed can`t be a single string." |
| 450 | ) |
| 451 | if isinstance(feed_dict[key], bytes): |
| 452 | feed_dict[key] = feed_dict[key].decode() |
| 453 | else: |
| 454 | if self.feed_types_[key] == bytes_type: |
| 455 | raise ValueError( |
| 456 | "feedvar is string-type,feed can`t be a single int or others." |
no test coverage detected